You are confusing fnmatch and regular expressions in your code. To get all files and directories except the special ones, use this:
$dir_array = array_diff($dir_array, array(".", ".."));
Alternatively, if you iterate the array anyway, you can test each element like this:
foreach ($dir_array as $name) {
if (($name != "..") && ($name != ".")) {
// Do stuff on all files and directories except . ..
if (is_dir($name)) {
// Do stuff on directories only
}
}
}
In php<5.3, you can exclusively use a callback function, too:
$dir_array = array_filter($dir_array,
create_function('$n', 'return $n != "." && $n != ".." && is_dir($n);'));
(See Allain Lalonde's answer for a more verbose version)
Since php 5.3, this can be written nicer:
$dir_array = array_filter($dir_array,
function($n) {return $n != "." && $n != ".." && is_dir($n);});
Finally, combining array_filter and the first line of code of this answer yields an (insignificantly) slower, but probably more readable version:
$dir_array = array_filter(array_diff($dir_array, array(".", "..")), is_dir);