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 the code

Then set the uploads disk address, which will be used to save the file. config / filesystem : disks

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

Finally, UploadController defines the upload function (using the disk method of the Storage to access the Uploads disk, as set in the previous Filesystem).

PutFile ($file, ‘test.png’, ‘20190705’, $file, ‘test.png’)

// Upload a single image publicfunction 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 successful' , 'data'= >$path];

                }

                else {

                    return ['code' => 400 , 'msg'= >'Upload failed']; }}}else {

            return ['code'= > 400,'msg'= >'Illegal request']; }} // Upload multiple images publicfunction 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 successful' , 'data'= >$path];

                }

                else {

                    return ['code' => 400 , 'msg'= >'Upload failed']; }}}else{

            return ['code'= > 400,'msg'= >'Illegal request']; }}Copy the code

Template upload operation to see the layui document bar, a hair the same operation!!

To learn more, please visit:

Tencent T3-T4 standard boutique PHP architect tutorial directory directory, as long as you finish the guarantee salary rise a step (continue to update)


I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc.