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

前端 未结 12 2667
悲哀的现实
悲哀的现实 2020-11-22 14:05

I have got user\'s avatars uploaded in Laravel storage. How can I access them and render them in a view?

The server is pointing all requests to /public,

12条回答
  •  旧巷少年郎
    2020-11-22 14:33

    It is good to save all the private images and docs in storage directory then you will have full control over file ether you can allow certain type of user to access the file or restrict.

    Make a route/docs and point to any controller method:

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

    When you will hit localhost:8000/docs file will be downloaded if any exists.

    The file must be in root/storage/app/users/documents directory according to above code, this was tested on Laravel 5.4.

提交回复
热议问题