How to use Storage Folder in Laravel

AuthorSumit Dey Sarkar

Pubish Date22 Mar 2023

categoryLaravel

In this tutorial we will learn how to use storage folder in laravel.

In Laravel, a storage folder is a directory that is used to store files that are not publicly accessible. You can use this folder to store uploaded files, cached data, and other data that you do not want to be visible to users.

 

How to use storage folder in laravel

Here are the steps to use the storage folder in Laravel:

 

Step 1 - Create a file storage folder:

The storage folder is located at the root of your Laravel application. If you do not have a storage folder, create one by running the following command in your terminal:

mkdir storage

 

Step 2 - Create subfolders inside the storage folder:

You can create subfolders inside the storage folder to organize your files. For example, if you want to store uploaded images, you can create a folder called "app/public/images". Run the following command to create the folder:

mkdir -p storage/app/public/images

 

Step 3 - Set file permissions:

You need to set file permissions for the storage folder to ensure that Laravel can write to it. Run the following command to set the correct permissions:

sudo chmod -R 775 storage

 

Step 4 - Use the storage folder:

To use the storage folder in your Laravel application, you can use the storage_path() helper function. For example, to get the path to the public images folder, you can use the following code:

$path = storage_path('app/public/images');

 

You can also use the Storage facade to interact with the storage folder. For example, to store a file in the public images folder, you can use the following code:

use Illuminate\Support\Facades\Storage;

Storage::putFile('public/images', $file);

 

These are the basic steps to use the storage folder in Laravel. You can refer to the Laravel documentation for more information on how to use the storage folder and its features.

 

Comments 0

Leave a comment