且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检索存储在存储文件夹中的图像

更新时间:2022-06-13 22:41:15

由于存储路径对于公众直接不可用,因此可以实现类似的目的.您需要在这样的路由中提供公共网址.

Somewhat like this you can achieve as storage path is directly unavailable for public. you need to provide public url in route like this.

可见

<img src="{{route('avatar',$filename)}}" />

<img src="/avatars/{{$filename}}" />

在routes/web.php中

In routes/web.php

Route::get('/avatars/{filename}', function ($filename)
{
    $path = storage_path() . '/avatars/' . $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;
})->name('avatar');