How to check if a folder exists before creating it in laravel?

With Laravel you can use:

$path = public_path().'/images';
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);

By the way, you can also put subfolders as argument in a Laravel path helper function, just like this:

$path = public_path('images/');

See: file_exists()

Usage:

if (!file_exists($path)) {
    // path does not exist
}

In Laravel:

if(!File::exists($path)) {
    // path does not exist
}

Note: In Laravel $path start from public folder, so if you want to check 'public/assets' folder the $path = 'assets'


You can also call this method of File facade:

File::ensureDirectoryExists('/path/to/your/folder')

which creates a folder if it does not exist and if exists, then does nothing