【问题标题】:Get image in storage in Laravel 5.2在 Laravel 5.2 中获取存储中的图像
【发布时间】:2017-04-17 11:06:19
【问题描述】:

我在storage->app->upload->images->1472803410756.jpg 中有一张图片。当我输入 url http://localhost:8012/demo/storage/app/upload/images/1472803410756.jpg 时,浏览器会准确显示我的图像。但是使用php artisan serve 运行,url:http://localhost:8000/storage/app/upload/images/1472803410756.jpg 浏览器找不到我的图像。怎么解决?

我的html代码:

<td><img class="table_image"  src="{{asset("storage/app/upload/images/$product->product_image")}}"></td>

【问题讨论】:

    标签: php frameworks laravel-5.2


    【解决方案1】:

    这是您从存储文件夹获取图像的错误方式。因为存储文件夹只有写入权限。所以,首先你需要创建存储路径的路由

    enter code here
    Route::get('storage/{filename}', function ($filename)
    {
        $path = storage_path('app/upload/images/' . $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;
    });
    

    之后你的 img 标签看起来像这样:-

    <td><img class="table_image"  src="{{url("storage/".$product->product_image)}}"></td>
    

    希望对你有帮助!

    【讨论】:

    • 很高兴为您提供帮助!不要忘记接受正确答案(y)
    猜你喜欢
    • 2016-09-05
    • 2019-12-24
    • 2015-09-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多