laravel框架多文件上传

1、首先先在laravel根目录下执行 $ php artisan storage:link ,产生映射文件 

2、编写controller层

// 文件上传
    public function upload(Request $request){

        // 判断是否有上传文件字段
        if ( $request->file() ){
            //定义一个空数组存放文件列表
            $files = []; 
            // 循环获取file字段传过来的文件
            foreach ($request->file() as $file){
                $obj = [];
                //将图片存储到了 ../storage/app/public/product/ 路径下
                $path = $file->store('public/product/'.date('Y-m-d'));
                $path = str_replace('public','',$path);
                $obj['url']= asset('storage'.$path);
                // 赋值给对象
                $files[] = $obj;
            }

            return $this->responseMsg( 0, 'success', $files);
        }else{
            return $this->responseMsg( -2, '请上传文件', null);
        }
    }

3、编写路由

// 文件上传
Route::post('upload', 'UtilsController@upload');

4、用 postman 模拟请求,注意红框部分,必须要两个不一样的字段才能产生两个文件

猜你喜欢

转载自www.cnblogs.com/zion0707/p/12084373.html
今日推荐