首页 > 解决方案 > 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 显示我的图像文件

标签: phplaravelfile-handling

解决方案


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

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

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

推荐阅读