上传到服务器后,Laravel 无法访问存储在存储文件夹中的图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49473436/
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
Laravel Cannot Access Image Stored in Storage Folder after Uploading in Server
提问by Ahsan
I am trying to upload and retrieve image in my Laravel project. It worked perfectly in my local server, but after uploading in live server, its not working.
我正在尝试在我的 Laravel 项目中上传和检索图像。它在我的本地服务器上运行良好,但在实时服务器中上传后,它无法正常工作。
I am using nginx.
我正在使用 nginx。
I have tried php artisan storage:link
but it says
我试过了,php artisan storage:link
但它说
The "public/storage" directory already exists
“public/storage”目录已经存在
I am using this line to open the stored image in a new page.
我正在使用这一行在新页面中打开存储的图像。
<a href="{{ url('/storage/images/'.$file->file_name) }}" title="">View</a>
If I place mouse on the view button, it shows this link: my_ip/storage/images/image.png
which I think is correct.
如果我将鼠标放在查看按钮上,它会显示此链接:my_ip/storage/images/image.png
我认为这是正确的。
However, clicking the link redirects to homepage.
但是,单击该链接会重定向到主页。
回答by mwal
The href
will be correct, but that's not the issue. The issue is the presence of the file at the location of the link. It's not there, as you're finding.
该href
会是正确的,但是这不是问题。问题是该文件存在于链接位置。它不存在,正如你所发现的。
When you upload, make sure you are not uploading a real directory at public/storage
.
上传时,请确保您上传的不是真实目录public/storage
.
On your local server, did you, by mistake, create a real directory at public/storage
?
在您的本地服务器上,您是否错误地在public/storage
.
If you did, you need to know that the laravelconvention is to store your files in the storage/app/public
directory in your app directory.
You don't create the folder public/storage
yourself.
You create a symlink to link it to there instead. That way, stuff you put in storage/app/public
, also appears, because of the symlink, at public/storage
.
如果你这样做了,你需要知道Laravel约定是将你的文件存储storage/app/public
在你的应用程序目录中的目录中。您不会public/storage
自己创建文件夹。您创建一个符号链接以将其链接到那里。这样,您放入storage/app/public
, 的内容也会出现,因为符号链接位于public/storage
。
First check your local server follows the laravel convention outlined above (and in the docs), then, after uploading to your server, try the storage:link
command again, and, so long as you don't have a real directory at public/storage
anymore, but just a link, it should hopefully work.
首先检查您的本地服务器是否遵循上面概述的 laravel 约定(以及在文档中),然后,在上传到您的服务器后,storage:link
再次尝试该命令,只要您不再有真正的目录public/storage
,而只是一个链接,它应该有希望工作。
Note what I've done here is interpret the error message you were getting about that directory already existing.
请注意,我在这里所做的是解释您收到的有关该目录已经存在的错误消息。
Also, check this answerif you are using Homestead. Instead of creating a storage link on the host computer, you should ssh into Vagrant and create a storage link here.
另外,如果您使用的是 Homestead ,请检查此答案。与其在主机上创建存储链接,不如通过 ssh 进入 Vagrant 并在此处创建存储链接。
回答by Oliver White
In case you have uploaded public/storage folder to server and getting error: The "public/storage" directory already exists.
如果您已将 public/storage 文件夹上传到服务器并出现错误:“public/storage”目录已存在。
just delete it with
只需删除它
rm -R public/storage
and run the command again
并再次运行命令
php artisan storage:link