使用 Laravel 访问前端上传的文件 [英] Accessing an uploaded file on frontend with Laravel
问题描述
我上传并存储图片.进入我的 blade.php 我有这一行:
I upload and store picture. Into my blade.php I have this line:
<img src="{{ storage_path('app\\'. $image) }}" />
此行生成此行:
<img src="C:\laragon\www\ProjectName\storage\app\uploads/images/IQeJo4w9lKqSxufTySpYKv1m9Z0TOV3PIYIvlN3g.jpeg" />
图像没有出现,但如果我复制/过去C:\
链接到我的浏览器 urlfield,我可以看到图像.
The image doesn't appears, but if I copy/past the C:\
link into my browser urlfield, I have the image visible.
你有什么想法用 img 标签显示图像吗?
Have you any idea to show the image with the img tag?
推荐答案
您将图像存储在 /storage/app/uploads
中,不是 公共目录.公共目录是/storage/app/public
.
You stored your image inside /storage/app/uploads
, which is not a public directory. The public directory is /storage/app/public
.
要将图像保存到公共磁盘,请在文件上传控制器中尝试:
To save an image to the public disk instead, try this in a file upload controller:
$path = $request->file('image')->storePublicly('uploads/images');
(保存$path
到数据库)
然后在您的刀片视图中:
Then inside your blade view:
<img src="{{ Storage::disk('public')->url($path) }}">
为此,您还需要链接公共存储磁盘(如果尚未链接):
For this to work, you also need to link the public storage disk if you have not already:
php artisan storage:link
<小时>
storage_path()
为您提供文件的内部路径,该路径不适用于创建指向可由网络服务器提供的图像的链接.
storage_path()
gives you the internal path to the file, which will not work for creating links to an image that can be served by a webserver.
这篇关于使用 Laravel 访问前端上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!