2017-10-11 17 views
0

routes.php文件Laravel 5.5 - 沒有創建符號鏈接訪問存儲的圖像 - 拋出400錯誤

Route::get('img/{filename}', '[email protected]')->name('files.show'); 

我FilesController

public function show($filename) 
{ 
    if (!Storage::exists('img/' . $filename)) 
    { 
     return 'error'; //file exist, this is never executes 
    } 
    $file = Storage::get('img/' . $filename); // this line breaks 
    dd($file); 

    return new Response($file, 200); 
} 

存儲::得到( 'IMG /' $文件名。 )在頁面上拋出錯誤400 ... 路徑是好的...

我不想公開鏈接,因爲我想圖像是私人的,只能通過控制器訪問...

+0

檢查該文件的權限。 – aynber

+0

我檢查了/ public/images文件夾中的圖像的權限... 我可以通過url訪問它們(例如:http:// localhost:8000/images/Dusan_RO8ujk2LVHuXH06F.png) – Dulo

回答

0

首先,您必須從控制器操作中返回response對象。但是,您在if中返回string

其次,你下載錯誤的響應。瀏覽器不理解你的迴應,因此你會得到400錯誤。用於下載文件使用response()->download(...)函數。

+0

if statement condition is false(它告訴該文件存在),所以如果語句永遠不會執行... 並且if語句中的響應僅用於測試目的... – Dulo

0

問題是我沒有在我的服務器上存儲圖像內容。

我只是存儲沒有內容的名稱。

以下代碼返回圖像。

public function show($filename) 
{ 
    $storage = Storage::disk('local'); 

    if (!$storage->exists($filename)) { 
     return 'error'; 
    } 

    $file = $storage->get($filename); 
    $type = $storage->mimeType($filename); 

    return new Response($file, 200, [ 
     'Content-Type' => $type 
    ]); 
} 

感謝您的幫助,反正:)

相關問題