1.配置上传文件的地址,在config目录filesystems.php的
'disks' => [ //路径1 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), ], /路径2 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ], //路径3 's3' => [ 'driver' => 's3', 'key' => env('AWS_KEY'), 'secret' => env('AWS_SECRET'), 'region' => env('AWS_REGION'), 'bucket' => env('AWS_BUCKET'), ], ],
2.接收文件保存
方法一
public function picture1(){ $file = Input::file('file'); if(request()->isMethod('post')){ if($file -> isValid()) { $ext = $file->getClientOriginalExtension(); $extension = $file -> getClientOriginalExtension(); $newImagesName=md5(time()).random_int(5,5).".".$extension; $filedir="storage/uploads"; $file->move($filedir,$newImagesName); } } return view('index/picture'); }
方法二
public function picture2(Request $request){ if ($request->isMethod('POST')) { $fileCharater = $request->file('file'); if ($fileCharater->isValid()) { //括号里面的是必须加的 //如果括号里面的不加上的话,下面的方法也无法调用的 //获取文件的扩展名 $ext = $fileCharater->getClientOriginalExtension(); //获取文件的绝对路径 $path = $fileCharater->getRealPath(); //定义文件名 $filename = date('Y-m-d-h-i-s').'.'.$ext; //存储文件。disk里面的public。总的来说,就是调用disk模块里的public配置 Storage::disk('public')->put($filename, file_get_contents($path)); } } return view('index/picture'); }