1. 程式人生 > >laravel5.6上傳圖片及顯示

laravel5.6上傳圖片及顯示

drive ray cover 大神 encode end real md5 over

借鑒大神博客:https://blog.csdn.net/tony_110/article/details/80105099
文檔:laravel5.6文檔 文件傳輸

在config下新建文件admin.php,定義上傳文件的路徑

‘upload_img_path‘                       =>‘app/public/img‘,//本地上傳圖片路徑
‘upload_file_path‘                       =>‘app/public/files‘//本地上傳文件路徑

在config/filesystems.php下定義

‘disks‘ => [
    ‘uploadimg‘=>[
        ‘driver‘=>‘local‘,
        ‘root‘=>storage_path(config(‘admin.upload_img_path‘))
    ],
    ‘uploadfiles‘=>[
        ‘driver‘=>‘local‘,
        ‘root‘=>storage_path(config(‘admin.upload_file_path‘))
    ],

    ‘local‘ => [
        ‘driver‘ => ‘local‘,
        ‘root‘ => storage_path(‘app‘),
    ],

    ‘public‘ => [
        ‘driver‘ => ‘local‘,
        ‘root‘ => storage_path(‘app/public‘),
        ‘url‘ => env(‘APP_URL‘).‘/storage‘,
        ‘visibility‘ => ‘public‘,
    ],

    ‘s3‘ => [
        ‘driver‘ => ‘s3‘,
        ‘key‘ => env(‘AWS_KEY‘),
        ‘secret‘ => env(‘AWS_SECRET‘),
        ‘region‘ => env(‘AWS_REGION‘),
        ‘bucket‘ => env(‘AWS_BUCKET‘),
    ],

前端顯示

{{ html()->form(‘POST‘, route(‘frontend.repair.repair.upload‘))
->attribute(‘enctype‘, ‘multipart/form-data‘)->attribute(‘files‘, ‘ture‘)->class(‘form-horizontal‘)->open() }}
(紅色標註的很重要)
Controller中
use Illuminate\Support\Facades\Storage;

public function sendmsg(SendmsgRequest $request)

{
$file = $request->file(‘cover‘);

//保存圖片begin
$rule = [‘jpg‘, ‘png‘, ‘gif‘];
if ($request->hasFile(‘cover‘)) {//驗證是否上傳圖片
$clientName = $file->getClientOriginalName();// 文件原名
$tmpName = $file->getFileName();
$realPath = $file->getRealPath();//臨時文件的絕對路徑
$entension = $file->getClientOriginalExtension();// 擴展名

if (!in_array($entension, $rule)) {
return ‘圖片格式為jpg,png,gif‘;
}
$newName = md5(date("Y-m-d H:i:s") . $clientName) . "." . $entension;//圖片重命名
$bool = Storage::disk(‘uploadimg‘)->put($newName, file_get_contents($realPath));//保存圖片
//return back();
//return json_encode([‘status‘ => 1, ‘filepath‘ => $newName]);
} else {
$idCardFrontImg = ‘‘;
//return json_encode($idCardFrontImg);
$newName=1;
}

//保存圖片end
}
根據文檔中的要求composer安裝了三項東西。

顯示時:
<th><img src="/storage/img/pic_name"></th><!--根據數據庫中報存的圖片名稱顯示圖片-->

laravel5.6上傳圖片及顯示