First, set the route to upload the file:
Route::post('upload/images'['as'=>'uploadImages','uses'=>'UploadController@uploadImages']); Route::post('upload/multiUpload'['as'=>'multiUpload','uses'=>'UploadController@multiUpload']);
Then set the disk address of uploads, and the saved file will be used. config / filesystem : disks
'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], 'uploads'=>[ 'driver'=>'local', 'root'=>public_path('uploads/'), ] ],
Finally, the UploadController defines the upload function (using the disk method of Storage to access the uploads disk, which is set in the previous filesystem file)
putFile method: manage the file to the specified storage location, for example, automatically generate the file name, or manually set ('20190705', $file,' test.png ')
//Upload a single picture 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' => 'Upload succeeded' , 'data' => $path]; } else { return ['code' => 400 , 'msg' => 'Upload failed']; } } } else { return ['code' => 400, 'msg' => 'Illegal request']; } } //Upload multiple images 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' => 'Upload succeeded' , 'data' => $path]; } else { return ['code' => 400 , 'msg' => 'Upload failed']; } } }else{ return ['code' => 400, 'msg' => 'Illegal request']; } }
Template upload operation to see the layui documents, a hair of the same operation!!!
For more information, please visit: