【问题标题】:Laravel fail to return imageLaravel 无法返回图片
【发布时间】:2019-01-25 20:11:05
【问题描述】:

我在公用文件夹以外的存储文件夹中有一个图像和一些文本文件。文件夹架构是 贮存 |->上传

并设置类似的路线

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 检索图像时,它会返回像这样的空白图像 blank image

但是当我尝试检索文本文件时,它会返回文件

当 mime 类型为 image/* 时,还尝试使用 Image::make($path)response()->file($path),但输出仍然相同.. 那么如何在不使用公共文件夹的情况下从 url 显示我的图像文件

【问题讨论】:

标签: php laravel file-handling


【解决方案1】:

这就是我的做法...我还检查了用户权限,但我根据您的需要对其进行了调整...

public function getFile()
{
    $path = storage_path($folder . DIRECTORY_SEPARATOR . $filename);

    return response()->file($path);
}

【讨论】:

  • 也使用了它,但没有得到文件。文件路径正确,文件也存在。
  • 打印路径时会发生什么?顺便说一句,我通常将路径存储在数据库中,而不仅仅是文件名......这不是我计算路线的方式......我只是将我的代码改编为你的。 ://
  • @erubile 它返回完整的文件路径`"E:\ecommerce\storage\uploads\7ef33eac189a8006c22af9ce66a2b096.jpg"`,当在资源管理器地址栏中输入此路径时,文件打开并在浏览器 url 中尝试带有文件协议文件:///E:/​​ecommerce/storage/uploads/7ef33eac189a8006c22af9ce66a2b096.jpg 的栏,它也工作得很好
  • 文件夹和所附内容有什么样的权限?每个人都有阅读权限吗?
  • 另外,我不认为我们使用相同的开发环境,我使用流浪宅基地......我的路径看起来不像那样
猜你喜欢
  • 2018-11-15
  • 2020-05-22
  • 2020-09-17
  • 2020-10-02
  • 2020-03-21
  • 2019-09-21
  • 2018-08-15
  • 2017-10-28
  • 1970-01-01
相关资源
最近更新 更多