什么是......的对应物:
if (!File::exists($path))
在 Laravel 5.1 中如何使用 Storage::
?
有人知道吗?
试试这个:
// 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::exists('/path/to/your/directory')) {
Storage::makeDirectory('/path/to/create/your/directory', 0775, true); //creates directory
}
makeDirectory()
只接受一个参数。下面是正确的用法:if (Storage::missing($storagePath)) Storage::makeDirectory($storagePath);
- Amir Hassan Azimi如果你想检查一个目录是否存在,可以尝试以下方法:
if (Storage::directories($directory)->has('someDirectory')) {
....
exists
只能在Laravel 5.2中使用,不是吗? - pravadirectories
函数返回一个数组,而不是一个集合(Collection),因此无法使用 has()
。 - DisgruntledGoat需要检查两件事情:(1)路径是否存在,(2)路径是否为目录。
下面的语句可以检查路径是否存在(适用于Laravel 5.2+),无论是文件还是目录:
Storage::exists('your-path') // bool
一旦您知道它的存在,这将确认该路径是一个目录:
Storage::getMetadata('your-path')['type'] === 'dir'
底层的Flysystem
库会在检查文件系统(可以是本地或远程)时缓存它所能缓存的内容,因此在正常情况下,这两个函数只会对文件系统进行一次调用。
if (!(Storage::disk('mydisk')->exists($path) && Storage::disk('mydisk')->getMetadata($path)['type'] === 'dir')) { echo "path '$path' is not a directory"; }
- Marten Koetsier使用存储门面的另一种laravel 5.5方法。
use Illuminate\Support\Facades\Storage;
if(Storage::exists('/mnt/files/file.jpg')) {
dd('file esxists');
} else {
dd('no file found');
}
在Laravel 8.x中,您可以使用以下条件:
use Illuminate\Support\Facades\Storage;
if (Storage::disk('public')->exists($path))
{
//
}
$exists = Storage::disk('local')->has('**dirname**');
if(!Storage::disk('public')->exists('your folder name'))
{
//what you want to do
}
$exists = Storage::disk('public')->exists('images/test_image.jpg');
其中的'public'
是在filesystem.php中进行配置的。'public' => [
'driver' => 'local',
'root' => public_path(),
'url' => env('APP_URL').'/public',
'visibility' => 'public',
],
'images/test_image.jpg'
是图片的路径。
File::isDirectory($YOURDIRECTORYPATHHERE);
,它将根据存在情况返回布尔值!
exists
函数可以很好地用于目录,所以->exists('dirname');
是可行的。 - DisgruntledGoat