Laravel Access Images outside public folder

后端 未结 3 1295
借酒劲吻你
借酒劲吻你 2021-01-23 02:05

I need to store images in a backend for logged in users. The stored images need to be protected and not visible from the outside (public). I choosed a \"storage\" folder for thi

3条回答
  •  不思量自难忘°
    2021-01-23 02:56

    You can do this like this:

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

    Reference:

    Laravel 5 - How to access image uploaded in storage within View?

    Or Alternatively you can use this library: https://github.com/thephpleague/glide

    Just use composer to install it in your project

    By default, this will render images from your storage, and allow you to do all sorts of things with it such as cropping, color correction etc.

    Reference:

    http://glide.thephpleague.com/

    https://laracasts.com/discuss/channels/laravel/laravel-5-how-can-we-access-image-from-storage?page=1

提交回复
热议问题