获取所有文件所有子文件夹和所有隐藏文件与glob

时间:2022-09-01 23:25:34

In my recurive function to zip a whole folders i have this piece of code glob($path. '/*') that give me all files and subfolders matching my $path.

在我的recurive函数中压缩整个文件夹我有这段代码glob($ path。'/ *')给我所有文件和子文件夹匹配我的$ path。

Here I read that with glob I can get even hidden files ".filename" with glob('{,.}*', GLOB_BRACE) How to merge in one expression my needs? I tried glob('{/,.}*', GLOB_BRACE) but only give me the files I tried glob('{/,.,}*', GLOB_BRACE) but I get crazy results

在这里,我用glob读取,我甚至可以使用glob('{,。} *',GLOB_BRACE)获取隐藏文件“.filename”如何合并我需要的一个表达式?我尝试了glob('{/,。}'',GLOB_BRACE),但只给了我试过的文件glob('{/,。,}}',GLOB_BRACE但是我得到了疯狂的结果

I already filteres . and ..

我已经过滤了。和..

How to merge

如何合并

glob($dir . '/*') 

and

    glob('{,.}*', GLOB_BRACE)

5 个解决方案

#1


8  

To get all folders/files (even the hidden ones):

获取所有文件夹/文件(甚至是隐藏的文件夹/文件):

$result = glob($path . '{,.}[!.,!..]*',GLOB_MARK|GLOB_BRACE);

This will prevent listing "." or ".." in the result.

这将阻止列出“。”或结果中的“..”。

#2


6  

Have you tried this?

你试过这个吗?

glob($path. '/{,.}*', GLOB_BRACE);

#3


1  

The glob() method returns an array. So if you want to merge two different glob results...

glob()方法返回一个数组。所以如果你想合并两个不同的glob结果......

$merged = array_merge(glob($dir . '/*'), glob('{,.}*', GLOB_BRACE));

#4


0  

Probably you've found already the solution, but in case you were looking for a way that gives you the files and directories, recursively and taking care of hidden files, this was what I got:

可能你已经找到了解决方案,但是如果你正在寻找一种方法来提供文件和目录,递归并处理隐藏文件,这就是我得到的:

function rglob($pattern, $flags = 0) {
    $files = glob($pattern, $flags);

    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
        $files = array_merge($files, $this->rglob($dir.'/'.basename($pattern), $flags));
    }

    return $files;
}

#5


0  

I am answering here in case anyone else is looking as this appears high on Google.

我在这里回答,以防其他人在看,因为这在谷歌看起来很高。

Solution 1 - glob only

解决方案1 ​​ - 仅限glob

This uses a glob that is tailored to skip '.' and '..' special directories. It matches anything that:

这使用了一个专门用于跳过'。'的glob。和'..'特殊目录。它匹配任何:

  • isn't hidden with a '.'
  • 没有隐藏'。'
  • is hidden with a '.' but is followed a non '.' character
  • 隐藏着'。'但是遵循非''。字符
  • starts with '..' but has at least one character after it
  • 以'..'开头,但后面至少有一个字符
$globbed = glob("{*,.[!.]*,..?*}", GLOB_BRACE);
var_dump($globbed);

Solution 2 - globignore

解决方案2 - globignore

This is a function to mimic the behaviour of globignore in bash.

这是一个模仿bash中globignore行为的函数。

function globignore(array $ignore, $glob, $glob_flags = 0)
{
    $globbed = glob($glob, $glob_flags);
    return array_filter($globbed, function ($f) use ($ignore)
    {
        $base = basename($f);
        foreach($ignore as $i)
        {
            if ($i == $base) return false;
        }
        return true;
    });
}
$globbed = globignore(['.','..'], "{*,.*}", GLOB_BRACE);
var_dump($globbed);

They appear to execute in almost exactly the same time on my system. Solution 1 requires less code but solution 2 is easier to include more terms to ignore.

它们似乎在我的系统上几乎完全相同的时间执行。解决方案1需要更少的代码,但解决方案2更容易包含更多要忽略的术语。

#1


8  

To get all folders/files (even the hidden ones):

获取所有文件夹/文件(甚至是隐藏的文件夹/文件):

$result = glob($path . '{,.}[!.,!..]*',GLOB_MARK|GLOB_BRACE);

This will prevent listing "." or ".." in the result.

这将阻止列出“。”或结果中的“..”。

#2


6  

Have you tried this?

你试过这个吗?

glob($path. '/{,.}*', GLOB_BRACE);

#3


1  

The glob() method returns an array. So if you want to merge two different glob results...

glob()方法返回一个数组。所以如果你想合并两个不同的glob结果......

$merged = array_merge(glob($dir . '/*'), glob('{,.}*', GLOB_BRACE));

#4


0  

Probably you've found already the solution, but in case you were looking for a way that gives you the files and directories, recursively and taking care of hidden files, this was what I got:

可能你已经找到了解决方案,但是如果你正在寻找一种方法来提供文件和目录,递归并处理隐藏文件,这就是我得到的:

function rglob($pattern, $flags = 0) {
    $files = glob($pattern, $flags);

    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
        $files = array_merge($files, $this->rglob($dir.'/'.basename($pattern), $flags));
    }

    return $files;
}

#5


0  

I am answering here in case anyone else is looking as this appears high on Google.

我在这里回答,以防其他人在看,因为这在谷歌看起来很高。

Solution 1 - glob only

解决方案1 ​​ - 仅限glob

This uses a glob that is tailored to skip '.' and '..' special directories. It matches anything that:

这使用了一个专门用于跳过'。'的glob。和'..'特殊目录。它匹配任何:

  • isn't hidden with a '.'
  • 没有隐藏'。'
  • is hidden with a '.' but is followed a non '.' character
  • 隐藏着'。'但是遵循非''。字符
  • starts with '..' but has at least one character after it
  • 以'..'开头,但后面至少有一个字符
$globbed = glob("{*,.[!.]*,..?*}", GLOB_BRACE);
var_dump($globbed);

Solution 2 - globignore

解决方案2 - globignore

This is a function to mimic the behaviour of globignore in bash.

这是一个模仿bash中globignore行为的函数。

function globignore(array $ignore, $glob, $glob_flags = 0)
{
    $globbed = glob($glob, $glob_flags);
    return array_filter($globbed, function ($f) use ($ignore)
    {
        $base = basename($f);
        foreach($ignore as $i)
        {
            if ($i == $base) return false;
        }
        return true;
    });
}
$globbed = globignore(['.','..'], "{*,.*}", GLOB_BRACE);
var_dump($globbed);

They appear to execute in almost exactly the same time on my system. Solution 1 requires less code but solution 2 is easier to include more terms to ignore.

它们似乎在我的系统上几乎完全相同的时间执行。解决方案1需要更少的代码,但解决方案2更容易包含更多要忽略的术语。