Get all file all subfolders and all hidden file with glob

别来无恙 提交于 2019-12-18 04:12:46

问题


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.

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

I already filteres . and ..

How to merge

glob($dir . '/*') 

and

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

回答1:


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:


Have you tried this?

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



回答3:


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

Solution 1 - glob only

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

  • 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

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

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.




回答4:


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

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



回答5:


This returns hidden files and folders, but not . or ..:

glob($dir . '/{,.}*[!.]', GLOB_MARK | GLOB_BRACE);

Instead of Houmams answer does it return the valid filname ...file.ext as well.

Tested with $dir="test/" and the following files:

test/.hiddenfolder/
test/folder/
test/...file
test/...file.ext
test/.h
test/.hiddenfile
test/file
test/file.ext

Additional information
Houman tried to target . or .. with [!.,!..], but as this is a character class it is not possible to target strings with a defined length. This means [!.] and [!..] are identical and target both strings not containing an unlimited amount of dots (., .., ..., ...., etc.). Because of that I used [!.] only. Targeting strings is only possible with curly braces like {jpg,png}. You find a good explanation in the php.net comments.




回答6:


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


来源:https://stackoverflow.com/questions/20906185/get-all-file-all-subfolders-and-all-hidden-file-with-glob

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!