如何从公共文件夹中获取图像文件作为 Laravel 中的响应?

Yog*_*lav 2 php laravel laravel-5.3

这些是我在控制器中尝试获取图像作为响应的不同方式。

我的图像驻留在public folder/img.

switch ($document->ext) {

    case 'doc':
        $img = File::get(url('/img/doc.png'));
        break;

    case 'docx':
        $img = File::get(asset('/img/docx.jpg'));
        break;

    case 'pdf':
        $img = File::get(public_path('/img/pdf.png'));
        break;

    case 'ppt':
        $img = File::get(url(public_path('/img/ppt.png')));
        break;

    default:
}
return new Response($img, 200);
Run Code Online (Sandbox Code Playgroud)

Yog*_*lav 9

我想这是一个更好的方法,以防我们改变 public_path

        case 'doc':
        return response()->file(public_path('img/doc.png'));
        exit;
Run Code Online (Sandbox Code Playgroud)