How to load images stored outside 'public' folder laravel?

by cortez.connelly , in category: PHP Frameworks , 6 months ago

How to load images stored outside 'public' folder laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lily , 6 months ago

@cortez.connelly 

In Laravel, the public folder is typically used to store publicly accessible files such as images, CSS, and JavaScript files. However, in some cases, you may want to load images stored outside the public folder. Here's one way to achieve it:

  1. Create a symbolic link: Open your command-line interface (CLI) and navigate to the root folder of your Laravel project. Run the following command to create a symbolic link from the public folder to your desired image storage location: php artisan storage:link
  2. Configure your filesystem: Open the config/filesystems.php file. Scroll down to the disks array. Create a new disk configuration for your out-of-public storage location. For example, if you have your images stored in the storage/app/images folder, add the following configuration: 'custom_images' => [ 'driver' => 'local', 'root' => storage_path('app/images'), 'url' => env('APP_URL').'/images', 'visibility' => 'public', ], This configuration tells Laravel to treat the storage/app/images folder as a disk named custom_images.
  3. Load the images: You can now use the Storage facade to load images from your out-of-public storage location. For example, to load an image with the filename image.jpg from the custom_images disk, you can use the following code: use IlluminateSupportFacadesStorage; $url = Storage::disk('custom_images')->url('image.jpg'); The $url variable will contain the URL that can be used to display the image in a web page.


By creating a symbolic link and configuring a custom disk, you can load images stored outside the public folder in Laravel.