how to check if directory exists with Storage:: facade in laravel?
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.
Chriz74
Updated on September 21, 2021Comments
-
Chriz74 over 2 years
What is the counterpart of:
if (!File::exists($path))
using
Storage::
in Laravel 5.1 ?Anyone?
-
prava almost 8 yearsI think
exists
will only work with Laravel 5.2. Isn't it? -
Alexey Mezenin almost 8 yearsNo, it's working since 5.0 (not sure about Laravel 4 though): laravel.com/docs/5.0/filesystem#basic-usage
-
prava almost 8 yearsBut, as per
https://laravel.com/docs/5.1/filesystem
, thehas
method may be used to determine if a given file exists on the disk. -
prava almost 8 yearsAnd as per
https://laravel.com/docs/5.2/filesystem
, theexists
method may be used to determine if a given file exists on the disk. -
Alexey Mezenin almost 8 years@prava, it's there: github.com/illuminate/filesystem/blob/master/Filesystem.php#L21
-
Chriz74 almost 8 yearsThanks but I am talking about a path not a file.
-
Chriz74 almost 8 yearstalking about a directory, not file.
-
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 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 over 7 yearsThe
directories
function returns an array, not a Collection, sohas()
cannot be used. -
DisgruntledGoat over 7 years@Chriz74 The
exists
function works fine for directories, so->exists('dirname');
will work. -
Ivanka Todorova almost 7 yearsDirectories are just files that contain files.
-
Jason about 6 yearsIronically, this is the only one that actually answers the question asked, though it is not an efficient way to do it.
-
Vahid Alvandi about 4 yearsthis create a folder by user id in ads disk you can change it to local or public
-
Marten Koetsier about 4 yearsThis 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 almost 3 yearsThe 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 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 almost 3 yearsHORRIBLY 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 over 2 yearsAs of at least Laravel 7, Storage::makeDirectory() only accepts the path as a parameter. laravel.com/docs/7.x/filesystem#directories