Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all file all subfolders and all hidden file with glob

Tags:

php

glob

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)
like image 888
newtphp Avatar asked Jan 03 '14 14:01

newtphp


2 Answers

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.

like image 170
mgutt Avatar answered Oct 05 '22 23:10

mgutt


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

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

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

like image 22
Houmam Avatar answered Oct 06 '22 00:10

Houmam