Laravel 5.4 显示 404 Not Found(公共存储)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51851912/
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 5.4 Showing 404 Not Found (Public Storage)
提问by DevGe
I have a question regarding to public storage and storage itself
我有一个关于公共存储和存储本身的问题
After i done inserting the files to the storage. then in the views page i want to display the files that i store to the storage. but i got problem i got error.
在我完成将文件插入存储后。然后在视图页面中,我想显示我存储到存储中的文件。但我有问题我有错误。
Failed to load resource: the server responded with a status of 404 (Not Found)
加载资源失败:服务器响应状态为 404(未找到)
- I've run php artisan storage:link
- The image exists in the public storage folder
- 我已经运行了 php artisan storage:link
- 图像存在于公共存储文件夹中
Image Exist in the public storage folder & I've run the php artisan storage link
图像存在于公共存储文件夹中,我运行了 php artisan 存储链接
This is my code, how can i insert the file to storage.
这是我的代码,如何将文件插入到存储中。
$filename = $request->file->getClientOriginalName();
$request->file->storeAs('storage',$filename);
This is my code, how to display the image to the view page
这是我的代码,如何将图像显示到视图页面
@foreach($home_slider as $get_data_slider)
@if($get_data_slider->slider_sorting == '1')
<a href="{{$get_data_slider->link}}" target="_blank"><img src="{{ asset('/public/storage/'.$get_data_slider->file.'') }}" class="d-block w-100"></a>
@endif
@endforeach
I found out that i can't access the localhost:8000/public/storage/1.jpg
我发现我无法访问 localhost:8000/public/storage/1.jpg
But when i use the localhost:8000/storage/1.jpg the file is showing. Showing image when the url is without public
但是当我使用 localhost:8000/storage/1.jpg 文件显示。 当 url 没有公开时显示图像
Thanks..
谢谢..
回答by Salman Zafar
instead of asset you should use:
而不是你应该使用的资产:
{{ Storage::url($pr->$get_data_slider->file) }}
for more info visit file System laravel
有关更多信息,请访问文件系统 laravel
回答by Véronne Yepmo
When you use the helper asset()
, you are directly in the public folder.
So your src should be
使用 helper 时asset()
,您直接位于 public 文件夹中。所以你的 src 应该是
src="{{asset('storage/'.$get_data_slider->file.'')}}".
Everything that is accessible to users from the browser is in the public folder, and for that reason you don't need to put 'public' in the url anymore. That is why the first url (localhost:8000/public/storage/1.jpg) is not found while the second one (localhost:8000/storage/1.jpg) displays your image. When you use the first one, it's like you have another folder named public (which contains the storage folder) into your public folder.
用户可以从浏览器访问的所有内容都在公共文件夹中,因此您不再需要将“公共”放在 url 中。这就是为什么找不到第一个 url (localhost:8000/public/storage/1.jpg) 而第二个 (localhost:8000/storage/1.jpg) 显示您的图像的原因。当您使用第一个时,就像您将另一个名为 public(其中包含存储文件夹)的文件夹放入您的公用文件夹中。
回答by Amitesh
asset()- Generate a URL to an application asset.
asset()- 生成应用程序资产的 URL。
url()- Generate a URL to an named route.
url()- 生成命名路由的 URL。
The asset() method is used to include CSS/JavaScript/images files, you can use it in this cases
asset() 方法用于包含 CSS/JavaScript/images 文件,您可以在这种情况下使用它
<link href="{{ asset('css/min.css') }}" rel="stylesheet">
<script src="{{ asset('use.typekit.net/zjb5wvv.js') }}"></script>
<img alt="logo" src="{{ asset('images/logo.png') }}">
The files must located in the publicfolder.
该文件必须位于该公共文件夹。
In your case :
在你的情况下:
<a href="{{$get_data_slider->link}}" target="_blank"><img src="{{ asset('storage/'.$get_data_slider->file.'') }}" class="d-block w-100"></a>
P.S: No need to explicitly add public folder inside asset();
PS:无需在 asset() 中显式添加公共文件夹;
回答by GMarco24
If you use Vagrant, php artisan storage:link
should be run under Vagrant. Otherwise, folder mapping would be wrong and that's one of the reasons why do you get 404.
So, first run vagrant ssh
, then find your project folder and run php artisan storage:link
.
如果你使用 Vagrant,php artisan storage:link
应该在 Vagrant 下运行。否则,文件夹映射将是错误的,这就是您得到 404 的原因之一。因此,首先运行vagrant ssh
,然后找到您的项目文件夹并运行php artisan storage:link
。