How to get images from storage folder in laravel?

if you want to store images or files secure on his server. so anyone can not access your images of files directly using url. that's the reason laravel introduce storage folder for file upload.

Solution 1:-

if you want to show image using asset function than first of all you need to link your storage folder using following artisan command:

php artisan storage:link


php artisan storage:link

That creates a symlink from public/storage to storage/app/public for you and that's all there is to it. Now any file in /storage/app/public can be accessed via a link like:

http://somedomain.com/storage/image.jpg

php artisan storage:link

<img src="{{ asset('productsimages/'.$article->image) }}" alt="" title=""></a>
// here productsimages is a directory under storage folder.
Note :- You don't need to write storage folder name.

If your images are going to be public you should probably move the folder into public.

storage/app/public/images

Then you can print them with

asset('storage/images/'.$article->image)


.env file

File name : .env

FILE_PATH = storage/

File name : index.php

On Server

Storing images or files in storage folder under this route on your server

public_html/domain/storage/app/public/images

On localhost

Storing images or files in storage folder under this route on your localhost

htdocs/domain/storage/app/public/images

File name : index.php

Now suppose you want to print an image in the localhost blade file.

<img src="{{'Storage'.$post->featured_image)}}" alt="">


and this will symlink the path to the actual image stored path under storage folder - storage/app/public/images

But this is not the case on shared hosting. An image needs another path.

To overcome this problem, we will make a constant in the config/app.php


'file_path' => env('FILE_PATH', '/'),
and in the .env file, set its value according to the hosting (localhost or server)

# For localhost
FILE_PATH = storage/

# For server
FILE_PATH = domain/storage/app/public/
Now you access the image

<div style="background-image:url({{asset(config('app.file_path').$item->image)}});">
As simple as that...





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial