先设置上传文件的路由:
Route::post('upload/images'['as'=>'uploadImages','uses'=>'UploadController@uploadImages']);
Route::post('upload/multiUpload'['as'=>'multiUpload','uses'=>'UploadController@multiUpload']);
再设置uploads磁盘地址,之后储存文件会使用到。config / filesystem : disks
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'uploads'=>[
'driver'=>'local',
'root'=>public_path('uploads/'),
]
],
最后UploadController定义上传函数(使用Storage的disk方法来访问uploads磁盘,就是前面filesystem文件里设置的)
putFile方法:管理文件到指定的存储位置,例为自动生成文件名,也可以手动设置 ('20190705', $file,'test.png')
//上传单张图
public function uploadImages(Request $request)
{
if ($request->isMethod('post')) {
$file = $request->file('file');
if($file->isValid()){
$path = Storage::disk('uploads')->putFile(date('Ymd') , $file);
if($path) {
return ['code' => 0 , 'msg' => '上传成功' , 'data' => $path];
}
else {
return ['code' => 400 , 'msg' => '上传失败'];
}
}
} else {
return ['code' => 400, 'msg' => '非法请求'];
}
}
//上传多张图
public function multiUpload(Request $request)
{
if($request->method('post')){
$files = $request->allFiles();
if(is_array($files)){
foreach($files as $file){
$path = Storage::disk('uploads')->putFile(date('Ymd') , $file);
}
if( $path ) {
return ['code' => 0 , 'msg' => '上传成功' , 'data' => $path];
}
else {
return ['code' => 400 , 'msg' => '上传失败'];
}
}
}else{
return ['code' => 400, 'msg' => '非法请求'];
}
}
模板的上传操作 自行看layui文档吧,一毛一样的操作!!!
更多学习内容请访问:
腾讯T3-T4标准精品PHP架构师教程目录大全,只要你看完保证薪资上升一个台阶(持续更新)
来源:oschina
链接:https://my.oschina.net/u/4398976/blog/4259526