Chriz74
Chriz74

Reputation: 1480

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

What is the counterpart of:

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

using Storage:: in Laravel 5.1 ?

Anyone?

Upvotes: 49

Views: 134266

Answers (16)

truefusion
truefusion

Reputation: 493

For those of us who use Storage::disk(), Laravel v9 appears to make use of directoryExists().

Storage::disk('name')->directoryExists($path);

Link to directoryExists method.

Upvotes: 1

Azimi
Azimi

Reputation: 9647

Try this:

if (Storage::missing($storagePath)) {
    Storage::makeDirectory($storagePath);
}

Upvotes: 0

Pejman Zeynalkheyri
Pejman Zeynalkheyri

Reputation: 4802

In Laravel 8.x you can use this condition:

use Illuminate\Support\Facades\Storage;

if (Storage::disk('public')->exists($path)) 
{
    // 
}

Upvotes: 4

tawfik al
tawfik al

Reputation: 34

try this code

 if (!file_exists(Storage::path($pathToDir))) {
  Storage::makeDirectory('path to directory', 0775, true); 
    // or any code
  }

Upvotes: -1

Vahid Alvandi
Vahid Alvandi

Reputation: 612

   if(!Storage::disk('ads')->exists(auth()->user()->id)) {
        Storage::disk('ads')->makeDirectory(auth()->user()->id, 0775, true); //creates directory
    }

Upvotes: 0

Mostafa Talebi
Mostafa Talebi

Reputation: 9183

You can easily use Storage::isDirectory(). Storage::class is an instance of \Illuminate\Filesystem\Filesystem and it contains a method isDirectory for checking if a given path exists and if it is directory or not.

    if(Storage::isDirectory("images")){
    // you code...
    }

Upvotes: 0

Shunhra Sarker
Shunhra Sarker

Reputation: 197

if(!Storage::disk('public')->exists('your folder name'))
{
    //what you want to do
}

Upvotes: 4

Tjeu Moonen
Tjeu Moonen

Reputation: 317

$exists = Storage::disk('local')->has('**dirname**');

Upvotes: 4

gjerich
gjerich

Reputation: 409

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');
}

Upvotes: 6

vmars
vmars

Reputation: 31

in laravel 5.4 $exists = Storage::disk('public')->exists('images/test_image.jpg'); - with 'public' that was config in filesystem.php

'public' => [
    'driver' => 'local',
    'root'   => public_path(),
    'url' => env('APP_URL').'/public',
    'visibility' => 'public',
],

'images/test_image.jpg' is the path of image.

Upvotes: 3

Well you can easily do that via File Facade File::isDirectory($YOURDIRECTORYPATHHERE); this will return boolean based on existence!

Upvotes: 2

Prateek
Prateek

Reputation: 1247

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

}

Upvotes: 30

Jason
Jason

Reputation: 4772

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.

Upvotes: 8

omarjebari
omarjebari

Reputation: 5519

You can retrieve all of the directories as an array then check if the directory (path) is there.

$dir = 'dir/path';
$existingDirs = Storage::disk(env('FILE_SYSTEM'))->allDirectories();
if (!in_array($dir, $existingDirs)) {
    // dir doesn't exist so create it
}

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163898

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

Upvotes: 11

prava
prava

Reputation: 3986

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');

Upvotes: 55

Related Questions