【发布时间】:2025-11-24 16:40:01
【问题描述】:
我被困住了。如何显示存储在 laravel 7.14 的 /storage/app/public 目录中的图像? 我已经尝试过我在堆栈溢出中找到的那些,甚至是我通过谷歌搜索找到的那些。
我试过这个解决方案
//Controller function
public function renderPic($filename) {
$path = '/storage/app/public/uploads/'.$filename;
if (!File::exists($path)) {
abort(404);
}
$file = File::get($path);
$type = File::mimeType($path);
$response = Response::make($file,200);
$response->header("Content-Type",$type);
return $response;
}
网络路由
Route::get('user/renderPic/{filename}', 'User@renderPic')->name('user.renderPic');
刀片视图文件
<img id="displayPic" src="{{ route('user.renderPic',$user->filename) }}">
不显示
【问题讨论】:
-
为什么直接从存储文件夹渲染?
-
这就是我问的原因,我不知道如何显示来自存储的图像
-
由于目录权限,我认为您不能直接从存储中显示,您应该创建一个符号链接。
-
你能不能至少展示一下如何做这样的事情,而不是像那样评论?谢谢
-
好的,我会写一个答案来尽可能地解释它。
标签: laravel laravel-7 laravel-7.x