Laravel部署至Byethost7服务器后文件存储失败及404问题求助
问题排查与修复步骤
1. 修正filesystems.php的public磁盘配置
你的public磁盘root路径配置错误,应该指向storage/app/public(符号链接对应的实际存储目录),而非直接使用storage_path()。修改后配置如下:
'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ], 'links' => [ public_path('storage') => storage_path('app/public'), ],
2. 修改文件存储逻辑,指定public磁盘
默认store()方法使用local磁盘,文件会被存在storage/app/images,不在符号链接的访问范围内。需要明确指定存储到public磁盘:
$product = new Product(); $image = $req->file('image'); // 第二个参数指定存储磁盘为public $image_url = $image->store('images', 'public'); $product->image_url = $image_url;
修改后文件会存入storage/app/public/images,符合符号链接的访问路径要求。
3. 修正视图中的图片路径
$image_url本身已经包含images/xxx.jpg格式的路径,原代码拼接后会出现重复的images层级,导致404。修改视图代码:
<img class="img-thumbnail" src="{{ asset('storage/'.$product->image_url) }}"/>
4. 重新生成符号链接(建议操作)
如果之前的符号链接存在异常,先删除public/storage目录,再重新执行:
php artisan storage:link
若Byethost服务器不允许执行artisan命令,可通过FTP手动创建符号链接:在public目录下创建storage文件夹,将其指向服务器上的../storage/app/public路径(注意匹配实际服务器目录结构)。
内容的提问来源于stack exchange,提问作者cevahir style
相关产品推荐
相关产品推荐

