Laravel 找不到存储文件夹中存储的图像

问题描述 投票:0回答:1

我目前正在尝试上传一些图像并用 Laravel 显示它们。存储在公共/图像中的所有图像都工作正常。无法找到该图像,但是在部署用于生产的 Linux 服务器上,上传的图像通过使用

php artisan storage:link
创建的符号链接显示在 Linux 终端的存储/应用程序/图像中 我的代码使用以下代码保存文件

    $this->validate($request, [
            'name' => 'required|max:255',
            'price' => 'required|numeric|gt:0',
            'promotion' => 'numeric|gt:-1',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048'
        ]);
        $fileName = time() . '.' . $request->image->extension();
        $request->image->storeAs('public/images', $fileName);

在保存到对象变量之前,该变量随后保存在 sqlite 数据库中。

我尝试使用

chmod 777 storage
更改存储文件夹权限,编辑要投入生产的
APP_ENV
变量,将我的
APP_URL
更新为实时网址,添加
FILESYSTEM_DRIVER=public
。我在每个步骤之后都反复清除了所有缓存和优化,并删除并重新链接了存储文件夹。

我的图片链接是这样的

<img src="{{ asset('storage/images/'.$dish->image) }}" />
。我尝试将其更改为 url 而不是 asset,并使用
Storage::url('storage/images/'.$dish->image)
作为 src。 当我输入精确的图像路径(我通过 linux 终端路径命令复制的)时,它会显示一个 404 页面,真正的问题是当我复制图像链接时,它是图像所在路径的精确副本
https://mysite/storage/images/1695812089.png

非常感谢任何帮助。

php image file-upload production-environment laravel-10
1个回答
0
投票

现在可以了,我将图像标签更改为这个 -

<img src="{{ Storage::disk('public')->url('images/'.$dish->image) }}" />

以及在 .htaccess 文件中添加此规则

RewriteRule ^storage/app/public/(.*)$ public/storage/$1 [L]

我的权限设置方式也与以前略有不同,我使用

chmod -R 755 storage
chmod -R 755 public/storage
当我之前没有包含
-R
时。

我很确定它的修复方法是将图像标签更改为从存储图像的公共驱动器调用,以及添加 .htaccess 规则以让应用程序知道存储已符号链接。

© www.soinside.com 2019 - 2024. All rights reserved.