65

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?

0

8 Answers 8

88

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'

1
  • Its much better to use File::ensureDirectoryExists(). It checks if directory exists and craete it if it does not
    – Sam
    Oct 2, 2022 at 7:16
45

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/');
2
  • 5
    I like elegant use of or in this answer.
    – tishma
    Jan 5, 2018 at 11:09
  • Works like a charm you just need to add use File; to the controller May 21, 2021 at 9:40
35

You can also call this method of File facade:

use Illuminate\Support\Facades\File;
File::ensureDirectoryExists('/path/to/your/folder')

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

3
  • 5
    this method is available since Laravel v6.13.0
    – Emanuel A.
    Oct 21, 2020 at 19:15
  • 1
    best solution for my problem! Mar 23, 2022 at 12:48
  • 5
    Namespace for File method: use Illuminate\Support\Facades\File;
    – Grant
    May 2, 2022 at 4:52
11

In Laravel 5.x/6 you can do it with Storage Facade:

use Illuminate\Support\Facades\Storage;

$path = "path/to/folder/";

if(!Storage::exists($path)){
    Storage::makeDirectory($path);
}
8

Way -1 :

if(!is_dir($backupLoc)) {

    mkdir($backupLoc, 0755, true);
}

Way -2 :

if (!file_exists($backupLoc)) {

    mkdir($backupLoc, 0755, true);
}

Way -3 :

if(!File::exists($backupLoc)) {

    File::makeDirectory($backupLoc, 0755, true, true);
}

Do not forget to use use Illuminate\Support\Facades\File;

Way -4 :

if(!File::exists($backupLoc)) {

    Storage::makeDirectory($backupLoc, 0755, true, true);
}

In this way you have to put the configuration first in config folder filesystems.php . [Not recommended unless you are using external disks]

5

The recommended way is to use

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

}

See the source code

If you look at the code, it's calling file_exists()

0

I normally create random folders inside the images for each file this helps a bit in encrypting urls and thus public will find it hardr to view your files by simply typing the url to your directory.

// Check if Logo is uploaded and file in random folder name -  
if (Input::hasFile('whatever_logo'))
            {
                $destinationPath = 'uploads/images/' .str_random(8).'/';
                $file = Input::file('whatever_logo');
                $filename = $file->getClientOriginalName();                
                $file->move($destinationPath, $filename);
                $savedPath = $destinationPath . $filename;
                $this->whatever->logo = $savedPath;
                $this->whatever->save();
            }

            // otherwise NULL the logo field in DB table.
            else 
            {
                $this->whatever->logo = NULL;    
                $this->whatever->save();    
            }            
0

This is what works great for me

if(!File::exists($storageDir)){
    File::makeDirectory($storageDir, 0755, true, true);
    $img->save('Filename.'.png',90);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.