環境
laravel5.5
Windows7/32-bit
Apache/2.4.23
PHP7.0.12
話不多說,直接上代碼
public function upImg(Request $request){
$respon = [
‘code’ => 400,
‘msg’ => ‘ERROR’,
‘data’ => ‘’
];
request->file(‘yc_img’)->isValid()){ //驗證圖片
if (in_array($request->file(‘yc_img’)->getMimeType(), $fileTypes)){ //判斷圖片類型
$path = $request->file(‘yc_img’)->store(’/public/pdf_img’); //文件存儲路徑
$path = str_replace(‘public’, ‘/storage’, $path); //文件可訪問路徑
}else{
$respon[‘msg’] = ‘圖片不合法|PNG|JPG|JPEG’;
}
}else{
$respon[‘msg’] = ‘圖片不存在’;
}
return $path; //返回圖片路徑
}
注意
需要開啓PHP擴展 php_fileinfo
需要創建軟連接 php artisan storage:link
ps
官方文檔 :https://learnku.com/docs/laravel/5.5/filesystem/1319