DevPremier
Ashwani Garg Laravel storage file not found on hosting solution, display image from the storage Posted at:

Laravel display image from the storage

Laravel display image from the storage

Laravel display image from the storage

Knowledge Requirements

Laravel Filesystem

Laravel provides a powerful filesystem abstraction. The Laravel Flysystem integration provides simple to use drivers for working with local filesystems, Amazon S3, and Rackspace Cloud Storage. Even better, it's amazingly simple to switch between these storage options as the API remains the same for each system.

Configuration

The filesystem configuration file is located at config/filesystems.php Within this file you may configure all of your "disks". Each disk represents a particular storage driver and storage location.

Example configurations for each supported driver are included in the configuration file. So, modify the configuration to reflect your storage preferences and credentials.

You may configure as many disks as you like, and may even have multiple disks that use the same driver.

The Public Disk

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

To create the symbolic link, you may use the storage: link Artisan command:

php artisan storage:link

The Local Driver

When using the local driver, all file operations are relative to the root directory defined in your configuration file.

By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:

Storage::disk('local')->put('file.txt', 'Contents');

Solution

These are some steps to print images in blade view files which are stored in the storage folder. And you just have to change a single constant in your .env file to differentiate the routes between your localhost server and shared hosting server.

By default, Laravel puts files in the directorystorage/app/public, which is not accessible from the outside web. 

I often get this question, how to display storage folder images in shared hosting.

Suppose:

   your laravel root directory is stored under public_html/domain/

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

 

Here, the role of this(php artisan storage:link) command comes into play, but this commands only works on localhost, which creates a problem on the shared hosting server. 

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...

Please share this article with your developer friends, it motivates us to make more tutorials like this.

Keep learning.