查看图片时出现404页面未找到的错误(Laravel)。

6

我有一张图片存储在数据库中,路径为 conventions/convention_title.jpg

该文件位于 "storage/app/conventions/convention_title.jpg"

当我使用如下代码 flyer }}"> 时,它并没有被显示出来...

或者

flyer }}"> 时,这张图片仍然不会被显示出来...

更糟糕的是,如果我尝试通过链接直接访问该图片,例如:

http://localhost:8000/storage/app/convention/convention_title.jpg ,我会收到一个视图未找到的错误 -> 404页面未找到。

这可能是什么原因?

3个回答

6
  1. php artisan storage:link
  2. 在编写过程中解决的问题
src=" {{ asset('/storage/'.$post->image) }}"

4

网页内容必须存储在 Laravel 根目录的 /public 文件夹中,您可以通过 asset 助手函数 轻松访问它。

例如:

/public/img/conventions/convention_title.jpg

然后您可以在视图中像这样访问它:

{{ asset("/img/conventions/convention_title.jpg")}}

如果您想测试是否可以访问该图片,则可以尝试:

http://localhost:8000/convention/convention_title.jpg

谢谢伙计...我该如何将图片上传到公共文件夹? - Inspired Prynce Kaka
你可以看一下@InspiredPrynceKaka的这个链接:https://laracasts.com/discuss/channels/laravel/image-upload-using-storage-function-to-public-folder?page=1 - Troyer
我没有意识到在asset()函数中不需要公共路径。非常感谢! - Randall Clintsman

2
今日免费次数已满, 请开通会员/明日再来
'disks' => [
    'local' => [
        'driver' => 'local',
        'root'   => storage_path(),
    ],
    'uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads',
    ],
]

然后使用它:
Storage::disk('uploads')->put('filename', $file_content);

或者您可以使用这种简单的方法。
$request->file('photo')->move(public_path("/uploads"), $newfilename);

然后,使用以上任何一种方法,您仍然可以使用asset()助手函数访问您的图像...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接