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

How to implement single file and multiple file upload in laravel

藏色散人
Release: 2020-04-27 11:59:22
forward
4015 people have browsed it

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']);
Copy after login

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/'),
        ]
    ],
Copy after login

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' => '非法请求'];
        }
    }
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Composer cannot install laravel
From 1970-01-01 08:00:00
0
0
0
Laravel Space/laravel-backup cannot be installed
From 1970-01-01 08:00:00
0
0
0
Laravel 5.1 Login laravel comes with it No more
From 1970-01-01 08:00:00
0
0
0
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template