所属分类:php教程
代码超简洁也超好理解呀!!! (欢迎补充~)
先设置上传文件的路由:
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文档吧,一毛一样的操作!!!
以上就是laravel单文件、多文件上传的实现方法的详细内容,更多请关注zzsucai网其它相关文章!