3 回答

TA貢獻1868條經驗 獲得超4個贊
我實際上并沒有嘗試過,但是我找到了Nginx auth_request模塊,該模塊允許您從Laravel檢查身份驗證,但仍然使用Nginx發送文件。
它向給定的URL發送內部請求,并檢查http代碼是否成功(2xx)或失?。?xx),如果成功,則讓用戶下載文件。
編輯:另一個選項是我嘗試過的東西,它似乎工作正常。您可以使用 X-Accel-Redirect-header從Nginx提供文件。該請求通過PHP進行,但不是通過發送整個文件,而是將文件位置發送到Nginx,然后Nginx將其提供給客戶端。

TA貢獻1936條經驗 獲得超7個贊
在上一個項目中,我通過執行以下操作來保護上傳:
創建的存儲磁盤:
config/filesystems.php
'myDisk' => [
'driver' => 'local',
'root' => storage_path('app/uploads'),
'url' => env('APP_URL') . '/storage',
'visibility' => 'private',
],
這會將\storage\app\uploads\無法上載的文件上傳到公眾。
要將文件保存在控制器上:
Storage::disk('myDisk')->put('/ANY FOLDER NAME/' . $file, $data);
為了使用戶查看文件并保護上傳內容免受未經授權的訪問。首先檢查磁盤上是否存在文件:
public function returnFile($file)
{
//This method will look for the file and get it from drive
$path = storage_path('app/uploads/ANY FOLDER NAME/' . $file);
try {
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;
} catch (FileNotFoundException $exception) {
abort(404);
}
}
服務的文件,如果用戶有權訪問:
public function licenceFileShow($file)
{
/**
*Make sure the @param $file has a dot
* Then check if the user has Admin Role. If true serve else
*/
if (strpos($file, '.') !== false) {
if (Auth::user()->hasAnyRole(['Admin'])) {
/** Serve the file for the Admin*/
return $this->returnFile($file);
} else {
/**Logic to check if the request is from file owner**/
return $this->returnFile($file);
}
} else {
//Invalid file name given
return redirect()->route('home');
}
}
最后在Web.php路由上:
Route::get('uploads/user-files/{filename}', 'MiscController@licenceFileShow');
添加回答
舉報