ItSolutionStuff.com

Laravel Check If Folder Exists Before Create Directory Example

By Hardik Savani • April 16, 2024
Laravel

In this post, i will help to check if directory exists or not before create in laravel 5 application. we can simply check if folder exist when you create new directory using File facade. File facade will provide method to check directory and create directory with permission using isDirectory() and makeDirectory().

we are always looking to create new folder dynamic when you need to store image according to dynamic database records on your laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 project.

isDirectory() will take one argument as folder path and return true if folder exist or false.

makeDirectory() will take four argument for create folder with permission.

I gave you simple controller method example to check if directory exist and create new one if not. Just check bellow example, it will create new folder "itsolutionstuff.com" on your upload folder in public folder.

Controller Method:

public function formSubmit(Request $request)

{

$path = public_path('upload/itsolutionstuff.com');

if(!File::isDirectory($path)){

File::makeDirectory($path, 0777, true, true);

}

dd('done');

}

Controller Method 2:

public function formSubmit(Request $request)

{

$path = public_path('upload/itsolutionstuff.com');

if(!Storage::exists($path)){

Storage::makeDirectory($path, 0777, true, true);

}

dd('done');

}

I hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Install JQuery UI in Laravel Vite?

Read Now →

How to Install JQuery in Laravel Vite?

Read Now →

Laravel Download File from URL to Storage Example

Read Now →

Laravel Call Function from Same Controller Example

Read Now →

How to Delete File from Public Folder / Storage Folder in Laravel?

Read Now →

Laravel 11 File Upload with Progress Bar Example

Read Now →

Laravel Create Word Document File using phpoffice/phpword package

Read Now →

How to Clear Log File using Command in Laravel?

Read Now →

How to Get File Size from Storage in Laravel?

Read Now →

Laravel Storage Dropbox Integration Example

Read Now →

How to Create Zip File in Laravel?

Read Now →

How to Check If File Exists or Not in Laravel?

Read Now →

How to Count Number of Files in a Directory in PHP?

Read Now →

How to Count Files in a Directory using Laravel?

Read Now →