Using Laravel's Storage class function exists()
you can quickly determine whether a directory is present or not. Additionally, the Storage class has a makeDirectory()
function that you can use to create folders and specify their permissions.
Check If Folder Exists Before creating Directory in Laravel
Create a directory with Storage Facades
Syntax
@method static bool makeDirectory(string $path)
Example
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
use Illuminate\Support\Facades\File;
if(!File::exists($path)) {
File::makeDirectory($path, 0777, true); //creates directory
}
Thank you for reading this article.