![](/img/trans.png)
[英]How to display images from user folder in public storage link on laravel
[英]Public link for a picture in storage sub folder in Laravel
當我保存圖像時,我嘗試保存在這樣的子文件夾中:
/2020/3/myimagename.jpg
我可以這樣保存文件:
$yearMonthPath = DIRECTORY_SEPARATOR.$currentYear.DIRECTORY_SEPARATOR.$currentMonth;
$fileName = $userId."_".uniqid()."_".$fileName;
Storage::disk('local')->put($yearMonthPath.DIRECTORY_SEPARATOR.$fileName, file_get_contents($file));
圖像文件現在位於:
/storage/app/2020/3/1_5e5fe9b93f37a_71a5d6e5c38bd2993c19a64229b383b1.jpg
我將存儲鏈接到公共:
php artisan storage:link
由於文檔,我的期望是獲得圖像的公共鏈接:
echo asset('storage/app/2020/3/'.$fileName);
這將輸出以下鏈接,該鏈接返回 404。
http://localhost:8000/storage/app/2020/3/1_5e5fe9b93f37a_71a5d6e5c38bd2993c19a64229b383b1.jpg
我還嘗試了不同的組合“public/2020...”、“public/storage/2020...”,但都沒有奏效。
我錯過了什么?
你需要public
磁盤。 public
磁盤用於將要公開訪問的文件。 默認情況下,公共磁盤使用local
驅動程序並將這些文件storage/app/public
在storage/app/public
。
Storage::disk('public')
存儲文件並創建符號鏈接后,您可以使用資產助手創建文件的 URL:
echo asset('storage/2020/3/'.$fileName); // remove `app`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.