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

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


当前回答

如果你像我一样,你以某种方式拥有完整的文件路径(我对所需的照片进行了一些glob()模式匹配,所以我几乎最终得到了完整的文件路径),并且你的存储设置链接良好(即,这样你的路径有字符串storage/app/public/),那么你可以使用我下面的小脏hack:p)

 public static function hackoutFileFromStorageFolder($fullfilePath) {
        if (strpos($fullfilePath, 'storage/app/public/')) {
           $fileParts = explode('storage/app/public/', $fullfilePath);
           if( count($fileParts) > 1){
               return $fileParts[1];
           }
        }

        return '';
    }

其他回答

无站点名称

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

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

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

如果你像我一样,你以某种方式拥有完整的文件路径(我对所需的照片进行了一些glob()模式匹配,所以我几乎最终得到了完整的文件路径),并且你的存储设置链接良好(即,这样你的路径有字符串storage/app/public/),那么你可以使用我下面的小脏hack:p)

 public static function hackoutFileFromStorageFolder($fullfilePath) {
        if (strpos($fullfilePath, 'storage/app/public/')) {
           $fileParts = explode('storage/app/public/', $fullfilePath);
           if( count($fileParts) > 1){
               return $fileParts[1];
           }
        }

        return '';
    }

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

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

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

根据Laravel 5.2文档,你的公共可访问文件应该放在目录中

storage/app/public

为了使它们可以从web上访问,你应该创建一个从public/storage到storage/app/public的符号链接。

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

现在你可以在你的视图中使用资产助手创建一个指向文件的URL:

echo asset('storage/file.txt');

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

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;
});