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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37134638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:21:56  来源:igfitidea点击:

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

filelaravelstorage

提问by Chriz74

What is the counterpart of:

什么是对应物:

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

using Storage::in Laravel 5.1 ?

使用Storage::在Laravel 5.1?

Anyone?

任何人?

回答by prava

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

回答by Prateek

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

}

回答by Alexey Mezenin

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/filesystem#directories

https://laravel.com/docs/5.1/collections#method-has

https://laravel.com/docs/5.1/collections#method-has

回答by Tjeu Moonen

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

回答by gjerich

Another way for laravel 5.5 using Storage Facade.

laravel 5.5 使用 Storage Facade 的另一种方式。

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

回答by vmars

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

在 Laravel 5.4 中 $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'is the path of image.

'images/test_image.jpg'是图像的路径。

回答by Jason

There are two things to check: (1) that the path exists, and (2) that the path is a directory.

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

This will check the path exists (syntax for Laravel 5.2+), no matter whether it is a file or a directory:

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

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 Flysystemlibrary 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.

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

回答by Mohammad Shoriful Islam Ronju

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

好吧,您可以通过 File Facade 轻松做到这一点,File::isDirectory($YOURDIRECTORYPATHHERE);这将根据存在返回布尔值!

回答by Shunhra Sarker

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

回答by Mostafa Talebi

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

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

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