Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to know if a folder exists before creating it, this is because I store pictures inside and I fear that the pictures are deleted if overwrite the folder. The code I have to create a folder is as follows

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

how can I do it?

like image 289
TuGordoBello Avatar asked Apr 24 '14 21:04

TuGordoBello


2 Answers

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'

like image 193
mister martin Avatar answered Nov 02 '22 09:11

mister martin


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/');
like image 41
bigbiggerpepe Avatar answered Nov 02 '22 09:11

bigbiggerpepe