Hujjat's avatar
Level 10

How to show uploaded images from storage dictory

Hi,

I have just watched the video of uploading file with laravel 5.3. Everything is working fine and I can upload file to storange/avatars directory. How can I show those files in views? I mean, previously we used to use asset() function which was searching in public directory, How can we show the files from storage/avatars directory?

Thank's

0 likes
7 replies
petrit's avatar

should create symbolic link

php artisan storage:link
gabrielbuzzi's avatar

I usually create a StorageController to render storage files, this way i can set who can access or not the file, using middlewares or some code.

Class StorageController extends Controller 
{
    public function load($directory, $file)
    {
        if ($path = $this->fileExists($directory, $file)) {
            $file = File::get($path);
            $type = File::mimeType($path);

            $response = Response::make($file, 200);
            $response->header("Content-Type", $type);

            return $response;
        }
    }
}
1 like
Hujjat's avatar
Level 10

@gabrielbuzzi Imagine if you want to show the images in app.blade.php file, How would you do that?

Thanks

gabrielbuzzi's avatar
<img src="action('StorageController@load', 'users', 'me.jpg')" />
1 like
gabrielbuzzi's avatar

@Hujjat you should put that inside the mustache

<img src="{{ action('StorageController@load', 'users', 'me.jpg') }}" />
Hujjat's avatar
Level 10

@gabrielbuzzi

Again I got error.

'''
    Action App\Http\Controllers\StorageController@load not defined. (View:  
    C:\Users\upplanet\Desktop\admin\resources\views\layouts\app.blade.php) (View:    
    C:\Users\upplanet\Desktop\admin\resources\views\layouts\app.blade.php)
    
'''

Here is my Controller

<?php

    namespace App\Http\Controllers;

    use Illuminate\Http\Request;

    use App\Http\Requests;

    class StorageController extends Controller
        {
                public function load($directory, $file){
                 if ($path = $this->fileExists($directory, $file)) {
                 $file = File::get($path);
                 $type = File::mimeType($path);

                    $response = Response::make($file, 200);
                    $response->header("Content-Type", $type);

                    return $response;
               }
         }
    }

'''

Please or to participate in this conversation.