how to check if directory exists with Storage:: facade in laravel?

83,042

Solution 1

Try this:

// To check if File exists in Laravel 5.1
$exists = Storage::disk('local')->has('file.jpg');

// To check if File exists in Laravel 5.2
$exists = Storage::disk('local')->exists('file.jpg');

Solution 2

If you want to check if a directory exists and create one if doesn't exist, this code will work for you.

if(!Storage::exists('/path/to/your/directory')) {

    Storage::makeDirectory('/path/to/create/your/directory', 0775, true); //creates directory

}

Solution 3

If you want to check for a directory, try this:

if (Storage::directories($directory)->has('someDirectory')) {
    ....

https://laravel.com/docs/5.1/filesystem#directories

https://laravel.com/docs/5.1/collections#method-has

Solution 4

Another way for laravel 5.5 using Storage Facade.

use Illuminate\Support\Facades\Storage;
if(Storage::exists('/mnt/files/file.jpg')) {
    dd('file esxists');
} else {
    dd('no file found');
}

Solution 5

There are two things to check: (1) that the path exists, and (2) that the path is a directory.

This will check the path exists (syntax for Laravel 5.2+), no matter whether it is a file or a directory:

Storage::exists('your-path') // bool

Once you know it exists, this will confirm the path is a directory:

Storage::getMetadata('your-path')['type'] === 'dir' 

The underlying Flysystem library will cache what it can when inspecting the filesystem (which may be local or remote), so in normal circumstances these two functions will only make one call to the filesystem.

Share:
83,042
Chriz74
Author by

Chriz74

Updated on September 21, 2021

Comments

  • Chriz74
    Chriz74 over 2 years

    What is the counterpart of:

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

    using Storage:: in Laravel 5.1 ?

    Anyone?

  • prava
    prava almost 8 years
    I think exists will only work with Laravel 5.2. Isn't it?
  • Alexey Mezenin
    Alexey Mezenin almost 8 years
    No, it's working since 5.0 (not sure about Laravel 4 though): laravel.com/docs/5.0/filesystem#basic-usage
  • prava
    prava almost 8 years
    But, as per https://laravel.com/docs/5.1/filesystem, the has method may be used to determine if a given file exists on the disk.
  • prava
    prava almost 8 years
    And as per https://laravel.com/docs/5.2/filesystem, the exists method may be used to determine if a given file exists on the disk.
  • Alexey Mezenin
    Alexey Mezenin almost 8 years
  • Chriz74
    Chriz74 almost 8 years
    Thanks but I am talking about a path not a file.
  • Chriz74
    Chriz74 almost 8 years
    talking about a directory, not file.
  • Chriz74
    Chriz74 almost 8 years
    @AlexeyMezenin ok but I read in the Storage API that it doesn't rely on directories because some services don't use it, like for example Amazon AWS which uses objects that can look as directories but are not directories, so how will the app behave on AWS if I check that, create the directory and put files into it?
  • Alexey Mezenin
    Alexey Mezenin almost 8 years
    @Chriz74, I do not use AWS, so I can't help you here. Your original question was about directories. You can try imy solution on AWS and tell us if it's working or not. )
  • DisgruntledGoat
    DisgruntledGoat over 7 years
    The directories function returns an array, not a Collection, so has() cannot be used.
  • DisgruntledGoat
    DisgruntledGoat over 7 years
    @Chriz74 The exists function works fine for directories, so ->exists('dirname'); will work.
  • Ivanka Todorova
    Ivanka Todorova almost 7 years
    Directories are just files that contain files.
  • Jason
    Jason about 6 years
    Ironically, this is the only one that actually answers the question asked, though it is not an efficient way to do it.
  • Vahid Alvandi
    Vahid Alvandi about 4 years
    this create a folder by user id in ads disk you can change it to local or public
  • Marten Koetsier
    Marten Koetsier about 4 years
    This actually works (and answers the question), also after selecting a disk. Oneliner: if (!(Storage::disk('mydisk')->exists($path) && Storage::disk('mydisk')->getMetadata($path)['type'] === 'dir')) { echo "path '$path' is not a directory"; }
  • Szczepan Hołyszewski
    Szczepan Hołyszewski almost 3 years
    The OP EXPLICITLY asked about directories. The task is NOT just to determine if "/foo/bar/something" exists in the filesystem, but ALSO if it is a directory.
  • Szczepan Hołyszewski
    Szczepan Hołyszewski almost 3 years
    @IvankaTodorova But regular files AREN'T, and the requirement here is to be able to determine WHETHER a given "file" is one that can "contain other files" OR not.
  • Szczepan Hołyszewski
    Szczepan Hołyszewski almost 3 years
    HORRIBLY INEFFICIENT. Reads entire containing directory, processes entries one by one and INTERNALLY checks whether they are directories (and somehow it MANAGES to do it, even though "AWS doesn't have concept of directories yadda yadda"), then returns all the ones that are directories, then YOU must check whether the item of interest is in the returned set.
  • Egnaro
    Egnaro over 2 years
    As of at least Laravel 7, Storage::makeDirectory() only accepts the path as a parameter. laravel.com/docs/7.x/filesystem#directories