无法访问存储在 Laravel 5.5 中的 Storage 文件夹中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46784608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not able to access image stored in Storage folder in Laravel 5.5
提问by Amrinder Singh
I am trying to access an image in the url which is stored in the /storage folder.
我正在尝试访问存储在 /storage 文件夹中的 url 中的图像。
Image Stored Path:
图像存储路径:
Project/storage/app/public/default.png
According to Laravel 5.5 docs, your publicly accessible files should be put in directory
根据 Laravel 5.5 文档,您的可公开访问的文件应放在目录中
storage/app/public
So I did the same, but when I try to access the image as:
所以我做了同样的事情,但是当我尝试访问图像时:
http://localhost:8000/storage/default.png
then it doesn't work.
那么它不起作用。
I tried the following command:
我尝试了以下命令:
php artisan storage:link
but it throws an error:
但它抛出一个错误:
The "public/storage" directory already exists.
“public/storage”目录已经存在。
回答by Damian Perez
As for 2019, if any of you got this error:
至于 2019 年,如果你们中有人遇到这个错误:
"The "public/storage" directory already exists."
““public/storage”目录已经存在。”
Then go to your public folder and delete the "storage" file.
然后转到您的公共文件夹并删除“存储”文件。
Once you do it, run again the "php artisan storage:link" and it will work now
完成后,再次运行“php artisan storage:link”,它现在可以工作了
回答by Yllndrit
Try deleting storage folder inside app/public/storage than run the command(php artisan storage:link
) again and than you can access file using this path
尝试删除 app/public/storage 中的存储文件夹,php artisan storage:link
然后再次运行命令(),然后您可以使用此路径访问文件
http://localhost:8000/storage/<folder_name_if_folder_exists>/<file_name>.<extension>
回答by aaron0207
You cannot access files inside storage folder directly as you tried. First you have to create a symbolic link of your storage "public" folder to your real "public" folder.
您无法直接访问存储文件夹中的文件。首先,您必须创建存储“公共”文件夹到真正“公共”文件夹的符号链接。
php artisan storage:link
Try again after the command.
命令后重试。
NOTE:If you are using windows open command line(cmd, git bash...) as adminto avoid errors.
注意:如果您以管理员身份使用 windows open命令行(cmd、git bash...)以避免错误。
回答by Hiren Gohel
First use php artisan storage:link
第一次使用 php artisan storage:link
Then you can access all public path using /* like:
然后您可以使用 /* 访问所有公共路径,例如:
http://localhost:8000/default.png
http://localhost:8000/default.png
Above command allow you to get images directly from public path!
以上命令允许您直接从公共路径获取图像!
Hope this helps you!
希望这对你有帮助!
回答by KingStevenNOS
The best way to do it is to first remove the symlink by running a code that will remove storage from the public folder first like so:
最好的方法是首先通过运行将首先从公用文件夹中删除存储的代码来删除符号链接,如下所示:
$ rm public/storage
then rerun the symlink code again
然后再次重新运行符号链接代码
$ php artisan storage:link
You will see that afterwards the public/storage folder will reappear and will be updated
您将看到之后 public/storage 文件夹将重新出现并更新
回答by Andrew Vink
Sep 2019
2019 年 9 月
What's working for me:
什么对我有用:
- SSH into your homestead VM
- cd into your project folder
- php artisan storage:link (As Admin in your terminal)
- Run successfully I don't see a /storage folder in my /public dir
- Access images via blade using storage/ in the url eg. "storage/{{ $post->image }}"
- 通过 SSH 连接到您的 Homestead VM
- cd 进入你的项目文件夹
- php artisan storage:link(作为终端中的管理员)
- 成功运行我在 /public 目录中没有看到 /storage 文件夹
- 使用 url 中的 storage/ 通过刀片访问图像,例如。“存储/{{ $post->image }}”
If you run into any errors:
如果您遇到任何错误:
- rm -rf public/storage
- php artisan storage:link (As Admin in your terminal)
- rm -rf 公共/存储
- php artisan storage:link(作为终端中的管理员)