If you scan a folder containing other folders AND files, how do you get rid of . and .. and files? How do you put in array only folders WITHOUT . and ..? I would like to use regular expression, but I’m newbie and I can’t get it right. My code is now this but doesn’t work:
if(fnmatch(‘\.{1,2}’,$dir_array[$i]) || is_file($dir_array[$i]){
unset($dir_array[$i]);
}else{ //other code
}
You are confusing fnmatch and regular expressions in your code. To get all files and directories except the special ones, use this:
Alternatively, if you iterate the array anyway, you can test each element like this:
In php<5.3, you can exclusively use a callback function, too:
(See Allain Lalonde’s answer for a more verbose version)
Since php 5.3, this can be written nicer:
Finally, combining array_filter and the first line of code of this answer yields an (insignificantly) slower, but probably more readable version: