借鑑大神博客:https://blog.csdn.net/tony_110/article/details/80105099
文檔:http://laravelacademy.org/post/8965.html
在config下新建文件admin.php,定義上傳文件的路徑php
'upload_img_path' =>'app/public/img',//本地上傳圖片路徑 'upload_file_path' =>'app/public/files'//本地上傳文件路徑
在config/filesystems.php下定義html
'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><!--根據數據庫中報存的圖片名稱顯示圖片-->
注:必定要執行
php artisan storage:link執行後public/storage文件夾上會出現連接的標誌,若是沒有則刪除storage文件夾,從新執行。