Article image

HOW TO ACCESS IMAGES FROM STORAGE FOLDER IN LARAVEL

Laravel -

Sep 27 2019

Dino Numic

Laravel uses public disk for files and images that should be publicly available. The default public disk uses the local driver and all files are stored in storage/app/public. However, storage folder is not publicly accessible from the web browser and because of that we will have to create a symbolic link from public/storage to storage/app/public.

You can look at a symbolic link as a pointer to a file or as a desktop shortcut to an executable located somewhere on a hard drive. If we edit, add, delete something from the storage it will reflect actions to the symlinked folder. It is useful to create symlinks when accessing same files from multiple locations such as pdf files, user uploaded images, and etc.

In Laravel we create symlink by running a following artisan command

php artisan storage:link

Once we have the symlink created we can generate an url to a file with Laravel asset() helper

asset('storage/images/avatar.jpg');

To generate a src for our image tag

<img src="{{ asset('storage/images/avatar.jpg') }}" alt="User avatar image" class="avatar">