php 如何在laravel中创建文件夹之前检查文件夹是否存在?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23280458/
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-08-25 16:37:24  来源:igfitidea点击:

How to check if a folder exists before creating it in laravel?

phplaravellaravel-4

提问by TuGordoBello

I need to know if a folder exists before creating it, this is because I store pictures inside and I fear that the pictures are deleted if overwrite the folder. The code I have to create a folder is as follows

在创建文件夹之前我需要知道文件夹是否存在,这是因为我将图片存储在其中,我担心如果覆盖文件夹,图片会被删除。我要创建文件夹的代码如下

$path = public_path().'/images';
File::makeDirectory($path, $mode = 0777, true, true);

how can I do it?

我该怎么做?

回答by mister martin

See: file_exists()

请参阅:file_exists()

Usage:

用法:

if (!file_exists($path)) {
    // path does not exist
}

In Laravel:

在 Laravel 中:

if(!File::exists($path)) {
    // path does not exist
}

回答by suarsenegger

With Laravel you can use:

使用 Laravel,您可以使用:

$path = public_path().'/images';
File::isDirectory($path) or File::makeDirectory($path, 0777, true, true);

By the way, you can also put subfolders as argument in a Laravel path helper function, just like this:

顺便说一句,您还可以将子文件夹作为参数放在 Laravel 路径辅助函数中,就像这样:

$path = public_path('images/');

回答by edi9999

The recommended way is to use

推荐的方法是使用

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

}

See the source code

查看源代码

If you look at the code, it's calling file_exists()

如果您查看代码,它正在调用 file_exists()

回答by Atiqur

Way -1 :

方式-1:

if(!is_dir($backupLoc)) {

    mkdir($backupLoc, 0755, true);
}

Way -2 :

方式-2:

if (!file_exists($backupLoc)) {

    mkdir($backupLoc, 0755, true);
}

Way -3 :

方式-3:

if(!File::exists($backupLoc)) {

    File::makeDirectory($backupLoc, 0755, true, true);
}

Do not forget to use use Illuminate\Support\Facades\File;

不要忘记使用 Illuminate\Support\Facades\File;

Way -4 :

方式-4:

if(!File::exists($backupLoc)) {

    Storage::makeDirectory($backupLoc, 0755, true, true);
}

In this way you have to put the configuration first in config folder filesystems.php . [Not recommended unless you are using external disks]

通过这种方式,您必须首先将配置放在 config 文件夹 filesystems.php 中。[除非您使用外部磁盘,否则不推荐使用]

回答by Adam

In Laravel 5.x/6 you can do it with Storage Facade:

在 Laravel 5.x/6 中,您可以使用Storage Facade 来实现

use Illuminate\Support\Facades\Storage;

$path = "path/to/folder/";

if(!Storage::exists($path)){
    Storage::makeDirectory($path);
}

回答by Keith Mifsud

I normally create random folders inside the images for each file this helps a bit in encrypting urls and thus public will find it hardr to view your files by simply typing the url to your directory.

我通常在每个文件的图像内创建随机文件夹,这有助于加密 url,因此公众将发现只需将 url 输入到您的目录中就很难查看您的文件。

// Check if Logo is uploaded and file in random folder name -  
if (Input::hasFile('whatever_logo'))
            {
                $destinationPath = 'uploads/images/' .str_random(8).'/';
                $file = Input::file('whatever_logo');
                $filename = $file->getClientOriginalName();                
                $file->move($destinationPath, $filename);
                $savedPath = $destinationPath . $filename;
                $this->whatever->logo = $savedPath;
                $this->whatever->save();
            }

            // otherwise NULL the logo field in DB table.
            else 
            {
                $this->whatever->logo = NULL;    
                $this->whatever->save();    
            }