File 如何检查目录是否与laravel中的Storage::facade一起存在?

File 如何检查目录是否与laravel中的Storage::facade一起存在?,file,laravel,storage,File,Laravel,Storage,什么是对应的: if (!File::exists($path)) 在Laravel 5.1中使用存储:: 有人吗?试试这个: // 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('f

什么是对应的:

if (!File::exists($path))
在Laravel 5.1中使用
存储::

有人吗?

试试这个:

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

如果要检查目录,请尝试以下操作:

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


您可以以数组的形式检索所有目录,然后检查目录(路径)是否存在

$dir = 'dir/path';
$existingDirs = Storage::disk(env('FILE_SYSTEM'))->allDirectories();
if (!in_array($dir, $existingDirs)) {
    // dir doesn't exist so create it
}
在laravel 5.4中
$exists=Storage::disk('public')->exists('images/test_image.jpg')
-使用filesystem.php中配置的
“public”

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

'images/test\u image.jpg'
是图像的路径。

需要检查两件事:(1)路径是否存在,以及(2)路径是否为目录

这将检查路径是否存在(Laravel 5.2+的语法),无论它是文件还是目录:

Storage::exists('your-path') // bool
Storage::getMetadata('your-path')['type'] === 'dir' 
一旦知道它存在,这将确认路径是一个目录:

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

底层的
Flysystem
库在检查文件系统(可能是本地的或远程的)时会缓存它所能缓存的内容,因此在正常情况下,这两个函数只会对文件系统进行一次调用。

如果要检查目录是否存在,如果不存在则创建一个目录,此代码将适用于您

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

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

}

您可以通过File Facade
File::isDirectory($YOURDIRECTORYPATHHERE)轻松实现这一点这将基于存在返回布尔值

laravel 5.5使用存储外观的另一种方式

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

您可以轻松地使用
存储::isDirectory()
Storage::class
\illumb\Filesystem\Filesystem
的一个实例,它包含一个方法
isDirectory
,用于检查给定路径是否存在以及是否为目录

if(存储::isDirectory(“图像”)){
//你的代码。。。
}
试试这段代码

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

我知道这太老了,无法回答,但您可以尝试:

is\u dir($path)


参考资料:

我认为
存在
只适用于Laravel 5.2。不是吗?不,它是从5.0开始工作的(但不确定Laravel4):但是,根据
https://laravel.com/docs/5.1/filesystem
has
方法可用于确定磁盘上是否存在给定文件。根据
https://laravel.com/docs/5.2/filesystem
exists
方法可用于确定磁盘上是否存在给定的文件。
directories
函数返回一个数组,而不是集合,因此
has()
不能使用。谈论的是目录,而不是文件。@Chriz74
exists
函数适用于目录,因此
->exists('dirname')
将起作用。目录只是包含文件的文件。具有讽刺意味的是,这是唯一一个真正回答问题的目录,尽管这不是一种有效的方法。这将在ads磁盘中按用户id创建文件夹,您可以将其更改为本地或公共。这实际上起作用(并回答问题),也可以在选择磁盘后进行。Oneliner:
如果(!(Storage::disk('mydisk')->存在($path)&&Storage::disk('mydisk')->getMetadata($path)['type']=='dir')){echo“path'$path'不是目录”}
 if (!file_exists(Storage::path($pathToDir))) {
  Storage::makeDirectory('path to directory', 0775, true); 
    // or any code
  }