首页 文章

Laravel 5 - 如何访问View中存储上传的图像?

提问于
浏览
108

我已经在Laravel存储中上传了用户的头像 . 如何访问它们并在视图中呈现它们?

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

8 回答

  • 227

    best 方法是在the answer below中创建一个非常好的@SlateEntropy symbolic link . 为了帮助解决这个问题,从版本5.3开始,Laravel includes a command使得这非常容易:

    php artisan storage:link
    

    这为你创建了一个从 public/storagestorage/app/public 的符号链接,这就是它的全部内容 . 现在 /storage/app/public 中的任何文件都可以通过以下链接访问:

    http://somedomain.com/storage/image.jpg
    

    如果出于任何原因,您无法创建符号链接(可能是您在共享主机上等),或者您想保护某些访问控制逻辑背后的某些文件,则可以选择使用特殊路径来读取和为图像服务 . 例如一个简单的闭包路线,如下所示:

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

    您现在可以像访问符号链接一样访问文件:

    http://somedomain.com/storage/image.jpg
    

    如果你正在使用Intervention Image Library,你可以使用它内置的 response 方法来使事情更简洁:

    Route::get('storage/{filename}', function ($filename)
    {
        return Image::make(storage_path('public/' . $filename))->response();
    });
    

    警告请记住,通过手动提供文件会导致性能下降,因为您要经历整个Laravel请求生命周期以便读取和发送文件内容,这比使用HTTP服务器处理要慢得多它 .

  • 13

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

    例如

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

    这也是Envoyer使用的方法,这是由Laravel的开发人员Taylor Otwell Build 的部署经理 .

  • 1

    如果您在Windows上,则可以在cmd上运行此命令:

    mklink /j /path/to/laravel/public/avatars /path/to/laravel/storage/avatars
    

    来自:http://www.sevenforums.com/tutorials/278262-mklink-create-use-links-windows.html

  • 3

    根据Laravel 5.2文档,您的可公开访问的文件应放在目录中

    storage/app/public
    

    要使它们可以从Web访问,您应该创建一个从 public/storagestorage/app/public 的符号链接 .

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

    现在,您可以使用资产助手在视图中创建文件的URL:

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

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

    php artisan storage:link
    

    然后在任何视图中,您都可以通过url helper访问您的图像 .

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

    最好将所有私有图像和文档保存在存储目录中,然后您将完全控制文件以太,您可以允许某些类型的用户访问该文件或限制 .

    制作路线/文档并指向任何控制器方法:

    public function docs() {
    
        //custom logic
    
        //check if user is logged in or user have permission to download this file etc
    
        return response()->download(
            storage_path('app/users/documents/4YPa0bl0L01ey2jO2CTVzlfuBcrNyHE2TV8xakPk.png'), 
            'filename.png',
            ['Content-Type' => 'image/png']
        );
    }
    

    当你点击 localhost:8000/docs 文件将被下载(如果存在) .

    根据上面的代码,该文件必须在 root/storage/app/users/documents 目录中,这是在 Laravel 5.4 上测试的 .

  • 45

    如果要加载少量的 Private 图像您可以将图像编码为base64并直接将它们回显到 <img src="{{$image_data}}">

    $path = image.png
    $full_path = Storage::path($path);
    $base64 = base64_encode(Storage::get($path));
    $image_data = 'data:'.mime_content_type($full_path) . ';base64,' . $base64;
    

    我提到私有,因为如果你不想存储可通过url公开访问的图像,你应该只使用这些方法,而你必须始终使用标准方式(链接存储/公共文件夹并使用HTTP服务器提供图像) .

    注意编码到 base64() 有两个重要的缺点:

    • 这会使图像尺寸增加约30% .

    • 您在一个请求中组合了所有图像大小,而不是并行加载它们,这对于某些小缩略图应该不是问题,但对于许多图像而言,避免使用此方法 .

  • 4

    如果您使用的是php,请使用php symlink函数,如下所示:

    symlink('/home/username/projectname/storage/app/public', '/home/username/public_html/storage')

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

相关问题