1 配置文件
config\filesystems.php
复制一个配置
‘public‘ => [
‘driver‘ => ‘local‘,
‘root‘ => storage_path(‘app/public‘),
‘url‘ => env(‘APP_URL‘).‘/storage‘,
‘visibility‘ => ‘public‘,
],
然后配置成如下形式:
//storage_path 指storage目录 ‘uploads‘ => [ ‘driver‘ => ‘local‘, ‘root‘ => storage_path(‘app/uploads‘), ],
2 创建路由
Route::any(‘/upload‘,[‘uses‘=>‘StudentController@upload‘]);
3 模板文件
\resources\views\student\upload.blade.php
@extends(‘layouts.app‘) @section(‘content‘) <div> <form method="POST" action="" enctype="multipart/form-data"> @csrf <div > <input type="file" name="source" id="file" > 请选择文件 <button type="submit"> 确认上传 </button> </div> </form> </div> @endsection
4 控制器
\app\Http\Controllers\StudentController.php
public function upload(Request $request) { if ($request->isMethod("POST")){ //var_dump($_FILES); $file = $request->file(‘source‘); //判断是否上传成功 if ($file->isValid()){ //获取原文件名 $originalName = $file->getClientOriginalName(); //获取扩展名 $ext = $file->getClientOriginalExtension(); //文件的类型 $file->getClientMimeType(); //临时文件的绝对路径 $realPath = $file->getRealPath(); //新文件名 $fileName = date(‘Y-m-d-H-i-s‘).‘-‘.uniqid().‘.‘.$ext; $bool = Storage::disk(‘uploads‘)->put($fileName,file_get_contents($realPath)); if($bool){ echo ‘上传成功‘; } } } return view(‘student.upload‘); }