我把用户的头像上传到了Laravel存储器里。我如何访问它们并在视图中呈现它们?

服务器将所有请求指向/public,那么如果它们在/storage文件夹中,我如何显示它们?


当前回答

你可以在控制台中运行这个命令来创建链接:

php artisan storage:link

其他回答

一种选择是在存储目录中的子文件夹和公共目录之间创建符号链接。

例如

ln -s /path/to/laravel/storage/avatars /path/to/laravel/public/avatars

这也是由Laravel的开发人员Taylor Otwell构建的部署管理器Envoyer所使用的方法。

对我来说,它与子文件夹路由一起工作

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

无站点名称

{{Storage::url($photoLink)}}

如果你想添加网站名称 附加在api JSON felids上的例子

 public function getPhotoFullLinkAttribute()
{
 Storage::url($this->attributes['avatar']) ;
}

如果你正在使用php,那么请使用php符号链接函数,如下所示:

symlink (' / home / spedfy projectname / storage / home app,为',/ / spedfy / public_html storage’)

将用户名和项目名更改为正确的名称。

首先,您需要使用artisan命令为存储目录创建一个符号链接

php artisan storage:link

然后在任何视图中,你都可以像这样通过url helper访问你的图像。

url('storage/avatars/image.png');