php - Laravel 无法返回图片
问题描述
我在公用文件夹以外的存储文件夹中有一个图像和一些文本文件。文件夹架构是存储 |-> 上传
并设置一条路线
Route::get('storage/{folder}/{filename}','FileController@getFile');
在控制器内部,getFile 方法具有以下代码
$path = storage_path($folder . DIRECTORY_SEPARATOR . $filename);
if (extension_loaded('fileinfo')) {
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;
}
else{
abort(412,'please enable the fileinfo extension or contact site administration');
}
当我尝试从 url 检索图像时,它返回空白图像,就像这个 空白图像
但是当我尝试检索文本文件时,它会返回文件
也尝试过Image::make($path)
,response()->file($path)
当 mime 类型为 image/* 时输出仍然相同.. 所以我如何在不使用公共文件夹的情况下从 url 显示我的图像文件
解决方案
我就是这样做的...我还检查了用户权限,但我根据您的需要对其进行了调整...
public function getFile()
{
$path = storage_path($folder . DIRECTORY_SEPARATOR . $filename);
return response()->file($path);
}
推荐阅读
- java - 更新 chrome 70 后未从选项卡创建的 chromedriver 会话崩溃
- python - PyCryptodome AES CBC 加密未提供所需的输出
- java - 是否可以停用现有消息通道的 Spring Integration 检查?
- java - 如何在会话中设置 B2B 单元(Hybris)?
- c# - WPF DataGridHyperlinkColumn 未触发 Click 事件
- javascript - 对象键的数字顺序从 0 javascript 重新排列
- c - 当它是 sizeof() 函数的参数时,为什么 n++ 不起作用?
- celery - 我可以对 celery 任务对象进行本地回调吗?
- ios - 使用 iOS 将 Intel Realsense D435 连接到 iPhone
- c++ - 使用 std::thread 在 C++ 中的单独线程中执行每个对象