找到 config/filesystems.php 文件而後修改 root、具體以下:php
'local' => [ 'driver' => 'local', // 'root' => storage_path('app'), 'root' => public_path() ], 'public' => [ 'driver' => 'local', // 'root' => storage_path('app/public'), 'root' => public_path(), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],
// 上傳頁面 Route::get('/upl', function(){ return view('Users.upload'); });
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>上傳</title> </head> <body> <form action="uplDo" method="post" enctype="multipart/form-data"> <!-- 文件上傳使用 post 提交方式、須要加 csrf 保護--> @csrf 文件上傳: <input type="file" name="file" id=""> <input type="submit" value="提交"> </form> </body> </html>
// 執行上傳 Route::post('/uplDo', 'UsersController@uplDo');
/** * 文件上傳 */ public function uplDo( Request $request ) { // 獲取上傳的文件 $file = $request->file('file'); // 得到上傳文件的原始名稱 getClientOriginalName // 040106.jpg echo $file->getClientOriginalName(); // 獲取上傳文件的文件擴展名 extension // jpg echo $extension = $file->extension(); // 獲取上傳文件的大小 // 29514 echo $file->getSize(); // 檢測上傳的文件是否合法,返回值爲true或false // 1 echo $file->isValid(); // 獲取上傳後保存的路徑 // php/NKtwzdR5l1zkeqKzc8YK36HpaB6TSdkRvBoyjlhq.jpeg echo $file->store('php'); // 最終的保存路徑是在 ../Laravel7/public/php/NKtwzdR5l1zkeqKzc8YK36HpaB6TSdkRvBoyjlhq.jpeg }