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)
This returns hidden files and folders, but not .
or ..
:
glob('{,.}*[!.]*', GLOB_MARK | GLOB_BRACE);
Example result:
file
file.
file.ext
folder/
...hiddenfile
...hiddenfile.ext
..hiddenfolder/
.h
.hiddenfile
.hiddenfolder/
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.
Although I used the flag GLOB_MARK
to add an ending slash to folders.
To get all folders/files (even the hidden ones):
$result = glob($path . '{,.}[!.,!..]*',GLOB_MARK|GLOB_BRACE);
This will prevent listing "." or ".." in the result.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With