Ryan
Ryan

Reputation: 304

(LARAVEL) I am simply trying to link to a photo in my storage/app/images directory

How do I link to my storage directory or should I store these somewhere else. I am creating a basic cms and would like to be able to show photos on each post. right now I am trying

<img src="{{ Storage::get('images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg') }}" 
        class="img-responsive" alt="Cinque Terre" width="304" height="236">

What happens right now is it returns absolutely nothing.

Upvotes: 0

Views: 76

Answers (1)

patricus
patricus

Reputation: 62278

Images must be stored in a directory that is publicly available to the world. The storage directory should not be publicly available.

You should create a folder somewhere under your /public directory and put your images there (for example /public/images). Then you can link to them like:

<img src="{{ asset('images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg') }}" class="img-responsive" alt="Cinque Terre" width="304" height="236">

Now, you can store images in the storage directory, but you would have to create a Laravel route that has logic to retrieve the data of the image and stream it back as image data, however that would add a lot of complexity just to view an image, and would mean having to load the entire Laravel framework for every image, which is not very efficient.

Edit

Since you're dealing with user uploaded images, you may want to follow some of the advice provided by the Laravel documentation. By default, the store($location) method will place the file in the storage/app/{$location} directory.

However, Laravel comes with a preconfigured public storage filesystem. If you pass in this filesystem as the second parameter, (store($location, 'public')), it will store the file in the storage/app/public/{$location} directory. To make this directory publicly available, you can run the storage:link artisan command, which will create a /public/storage symbolic link to your /storage/app/public directory.

php artisan storage:link

So, if your code is $request()->file('photo')->store('images', 'public'), it will store the photo as /storage/app/public/images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg and you access via {{ asset('storage/images/53bb15cceac4f7ecf9fb51cb78296f68.jpeg') }} (assuming you've run the artisan command to setup the symbolic link).

Documentation on storing uploaded files
Documentation on the public filesystem disk

Upvotes: 1

Related Questions