Skip to content

Laravel – Check If Directory Exists Before Create Directory

Last updated on March 31, 2022

You can easily check if a directory exists or not using Laravel’s Storage class method, exists(). And there is a makeDirectory() method in Storage class, by using it, you can create a folder and you can set permissions to the folder.

Create a directory with Storage Facades

Syntax

@method static bool makeDirectory(string $path)

Example script:

use Illuminate\Support\Facades\Storage;
if(!Storage::exists($path)) {
    Storage::makeDirectory($path); //creates directory
}

Create a directory with File Facades

Syntax

@method static bool makeDirectory(string $path, int $mode = 0755, bool $recursive = false, bool $force = false)

Example script:

use Illuminate\Support\Facades\File;
if(!File::exists($path)) {
    File::makeDirectory($path, 0777, true); //creates directory
}
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments