Reputation: 1480
What is the counterpart of:
if (!File::exists($path))
using Storage::
in Laravel 5.1 ?
Anyone?
Upvotes: 49
Views: 134266
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
Reputation: 9647
Try this:
if (Storage::missing($storagePath)) {
Storage::makeDirectory($storagePath);
}
Upvotes: 0
Reputation: 4802
In Laravel 8.x you can use this condition:
use Illuminate\Support\Facades\Storage;
if (Storage::disk('public')->exists($path))
{
//
}
Upvotes: 4
Reputation: 34
try this code
if (!file_exists(Storage::path($pathToDir))) {
Storage::makeDirectory('path to directory', 0775, true);
// or any code
}
Upvotes: -1
Reputation: 612
if(!Storage::disk('ads')->exists(auth()->user()->id)) {
Storage::disk('ads')->makeDirectory(auth()->user()->id, 0775, true); //creates directory
}
Upvotes: 0
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
Reputation: 197
if(!Storage::disk('public')->exists('your folder name'))
{
//what you want to do
}
Upvotes: 4
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
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
Reputation: 694
Well you can easily do that via File Facade File::isDirectory($YOURDIRECTORYPATHHERE);
this will return boolean based on existence!
Upvotes: 2
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
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
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
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
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