Laravel - 保存在存储文件夹中的图像不显示给用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50395449/
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 - Image saved in storage folder not showing to user
提问by Adokiye Iruene
I have this code to save an image in the storage/app/uploads folder
我有这个代码在 storage/app/uploads 文件夹中保存图像
$image_main = Image::where('property_id', $id)->get();
$file = $request->file('file');
$destinationPath = 'uploads';
$i = 0;
foreach ($file as $file1) {
$i++;
$extension = $file1->getClientOriginalExtension();
$path = $file1->storeAs(
$destinationPath, $i.time().".".$extension
);
This is my blade file
这是我的刀片文件
@foreach ($image_main as $images)
<img src="{{"/storage/app/".$images->filename}}
The file saves in the storage/app/uploads folder but it doesn't show to the user, it says file not found. am I to set anything in the filesystems.php file
该文件保存在 storage/app/uploads 文件夹中,但没有向用户显示,它说找不到文件。我要在 filesystems.php 文件中设置任何内容吗
回答by pirmax
Your storage folder is not accessible from user.
用户无法访问您的存储文件夹。
You must pass by Storage::url()method:
您必须通过Storage::url()方法:
Storage::url("/storage/app/{$images->filename}")
As:
作为:
<img src="{{ Storage::url("/storage/app/{$images->filename}") }}" alt="{{ $images->filename }}" />
回答by Dharma Saputra
Laravel storage filesystem is so unique. When you doing any upload it will upload in the storage/app/public
directory. To make it accessible in public
directory, things need to do is create symlink by run command:
Laravel 存储文件系统是如此独特。当您进行任何上传时,它将上传到storage/app/public
目录中。要使其在public
目录中可访问,需要做的是通过运行命令创建符号链接:
php artisan storage:link
This command will symlinked storage/app/public
to public/storage
此命令将符号链接storage/app/public
到public/storage
Just follow the documentationabout how to put and how to retrieve the file url. I am pretty sure the step you are missing is creating the symlink.
只要按照文档如何把以及如何检索文件的URL。我很确定您缺少的步骤是创建符号链接。
Hope it helps.
希望能帮助到你。
回答by Vikas Rinvi
If you are using homestead
as the environment on Mac then you need to follow this:
如果您homestead
在 Mac上使用作为环境,那么您需要遵循以下步骤:
- Delete your public/storage folder (if any)
Follow these steps:
- ssh into your homestead using below command
cd ~homestead
vagrant up
vagrant ssh
cd Code/PATH_TO_YOUR_APP_ROOT_DIR
php artisan storage:link
Access image like this
{{ asset('storage/img/myimage.jpg') }}
- 删除您的公共/存储文件夹(如果有)
按着这些次序:
- 使用以下命令通过 ssh 进入您的家园
cd ~homestead
vagrant up
vagrant ssh
cd Code/PATH_TO_YOUR_APP_ROOT_DIR
php artisan storage:link
像这样访问图像
{{ asset('storage/img/myimage.jpg') }}
回答by Iyathurai Iyngaran
You can't use the storage folder for this. You need to move the images to the public folder to access from http.
您不能为此使用存储文件夹。您需要将图像移动到公共文件夹才能从 http 访问。
Try to create a folder call images in public folder and upload the image to that folder. Then you can access the images as follows.
尝试在公共文件夹中创建一个文件夹调用图像并将图像上传到该文件夹。然后您可以按如下方式访问图像。
<img src="{{ public_path('images/image-name.png')}}" alt="" />