Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
In this tutorial, I will give you an example of “How to store image in storage with a custom folder in Laravel”, so you can easily apply it to your Laravel 5, Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10 applications.
The need to store images in storage with a custom folder in Laravel application for a smaller or a larger application, like CMS, ERP, or e-commerce application we can store images separately following a folder structure. It is easy to manage and easy to identify image galleries in the Laravel application.
Laravel’s filesystem configuration file is located at config/filesystems.php. Within this file, you may configure all of your filesystem “disks”. Each disk represents a particular storage driver and storage location. Example configurations for each supported driver are included in the configuration file so you can modify the configuration to reflect your storage preferences and credentials.
We will create a storage link for students (avatars and id_images) folder, using the filesystem in our local Laravel application storage folder.
Update config/filesystems.php
<?php return [ /* |-------------------------------------------------------------------------- | Default Filesystem Disk |-------------------------------------------------------------------------- | */ 'default' => env('FILESYSTEM_DISK', 'local'), /* |-------------------------------------------------------------------------- | Filesystem Disks |-------------------------------------------------------------------------- | */ 'disks' => [ 'local' => [ 'driver' => 'local', 'root' => storage_path('app'), 'throw' => false, ], 'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', 'throw' => false, ], 'avatars' => [ 'driver' => 'local', 'root' => storage_path('app/public/student/avatars'), 'url' => env('APP_URL').'/storage/student/avatars', 'visibility' => 'public', 'throw' => false, ], 'id_images' => [ 'driver' => 'local', 'root' => storage_path('app/public/student/id_images'), 'url' => env('APP_URL').'/storage/student/id_images', 'visibility' => 'public', 'throw' => false, ], ], /* |-------------------------------------------------------------------------- | Symbolic Links |-------------------------------------------------------------------------- | */ 'links' => [ public_path('storage') => storage_path('app/public'), public_path('storage/student/avatars') => storage_path('app/public/student/avatars'), public_path('storage/student/id_images') => storage_path('app/public/student/id_images'), ], ];
Link storage folder with new Disk
After defining the disk and links in the filesystem.php file, just hit the storage link command.
php artisan storage:link
After the storage: link command, you can check your storage folder (storage\app\public) folder there you will find new linked folders (student\avatars and student\id_images).
Update Controller or a Component File
Now you can store images in a custom folder in storage in the Laravel application.
In the controller or a component file, whenever you store an image, just define your disk name, which we created on the filesystem.php file.
$path = Storage::disk('avatars')->put($name,$img);
$path = Storage::disk('id_images')->put($name,$img);
I hope that this article helped you learn How to manage storage with a custom folder in the Laravel application. You may also want to check out our guide on How to manage different headers and footers on each page in Laravel.