简体   繁体   English

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

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

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?我该怎么做?

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
}

Note: In Laravel $path start from public folder, so if you want to check 'public/assets' folder the $path = 'assets'注意:在 Laravel 中$pathpublic文件夹开始,所以如果你想检查'public/assets'文件夹$path = 'assets'

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

You can also call this method of File facade:你也可以调用 File 门面的这个方法:

File::ensureDirectoryExists('/path/to/your/folder')

which creates a folder if it does not exist and if exists, then does nothing如果它不存在,则创建一个文件夹,如果存在,则什么也不做

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

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 .这样,您必须将配置首先放在配置文件夹 filesystems.php 中。 [Not recommended unless you are using external disks] [不推荐,除非您使用外部磁盘]

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()

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();    
            }            

This is what works great for me这对我很有用

if(!File::exists($storageDir)){
    File::makeDirectory($storageDir, 0755, true, true);
    $img->save('Filename.'.png',90);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM