Home  >  Article  >  PHP Framework  >  How to implement single file and multiple file upload in laravel

How to implement single file and multiple file upload in laravel

藏色散人
藏色散人forward
2020-04-27 11:59:093847browse

The following tutorial column of laravel will introduce to you the implementation method of single file and multiple file upload in laravel. I hope it will be helpful to friends in need!

How to implement single file and multiple file upload in laravel

#The code is super concise and easy to understand! ! ! (Welcome to add~)

First set the route for uploading files:

Route::post('upload/images'['as'=>'uploadImages','uses'=>'UploadController@uploadImages']);  
 Route::post('upload/multiUpload'['as'=>'multiUpload','uses'=>'UploadController@multiUpload']);

Then set the uploads disk address, which will be used to store files later. config / filesystem : disks

 'disks' => [
 
        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
        ],
 
        'uploads'=>[
            'driver'=>'local',
            'root'=>public_path('uploads/'),
        ]
    ],

Finally UploadController defines the upload function (use the disk method of Storage to access the uploads disk, which is set in the previous filesystem file)

putFile method: manage files to the specified The storage location, for example, the file name is automatically generated, or it can be set manually ('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' => '非法请求'];
        }
    }

The last and last: the upload operation of the template...see the layui document for yourself Well, it’s exactly the same operation! ! !​

The above is the detailed content of How to implement single file and multiple file upload in laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete