簡體   English   中英

Laravel從存儲中檢索圖像並且無法打開流:沒有這樣的文件或目錄

[英]Laravel retrieve image from storage and failed to open stream: No such file or directory

我在Laravel檢索圖像時遇到問題,並且無法在控制器中使用這些圖像。

我首先檢查以確保圖像存在並返回true

var_dump(storage::disk('local')->exists('image.png'))

如果我這樣做

$file = Storage::disk('local')->get('image.png');
var_dump(pathinfo($file));

我得到這個輸出

array(2) {
  ["basename"]=>
  string(0) ""
  ["filename"]=>
  string(0) ""
}

只是var_dump() $file給我一個空字符串

如果我這樣做

$file = Storage::disk('local')->url('image.png');
var_dump(pathinfo($file));

我得到

array(4) {
  ["dirname"]=>
  string(8) "/storage"
  ["basename"]=>
  string(9) "image.png"
  ["extension"]=>
  string(3) "png"
  ["filename"]=>
  string(5) "image"
}

我不想知道屬性,我需要能夠使用實際存儲的圖像並將其上傳到另一個API。

如果我嘗試去做

echo '<img src="'.$file.'">';

我在瀏覽器源代碼中看到了此圖片,但看不到任何圖像

<img src="/storage/image.png">

我用來上傳圖片的API返回了

mime_content_type(/storage/image.png): failed to open stream: No such file or directory

我堅信一旦我能獲得實際的圖像,其余的一切都會起作用

編輯:

我已經運行此命令php artisan storage:link

如果我使用$file = public_path('uploads/image.png'); 並將文件復制到公共文件夾(不是存儲/應用程序/公共)中,我可以根據需要使用圖像

這是不理想的,因為我不希望這些圖像可以公開訪問。 我需要能夠從存儲文件夾訪問圖像。

如果我運行$file = Storage::disk('public')->get('image.jpg'); 我得到mime_content_type(): Invalid path

我很想嘗試將圖像的路徑發送到我需要使用的API。 它只需要/the/path/to/the/image

我嘗試過的其他代碼在嘗試獲取圖像時會導致403

公用目錄外部的/storage文件夾,即Web服務器的文檔/ Web根目錄。 公眾無法訪問/storage文件夾。 您應該做出響應以輸出圖像內容。

$path = storage_path() . "/app/image.png";
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file, 200);
$response->header("Content-Type", $type);
return $response;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM