Adriana
Adriana

Reputation: 8624

How to get rid of . and .. while scaning the folder creating an array in php?

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
}

Upvotes: 2

Views: 296

Answers (7)

za_al
za_al

Reputation: 111

        $pathsArr = array();
    foreach (explode($dirSeparator, $currentPath) as $path) {
        if (strlen($path) && $path !== '.') {
            if ($path === '..') {
                // die('.....');
                array_pop($pathsArr);
            } else {
                $pathsArr[] = $path;
            }
        }
    }

    $realpath = $winDrive . $dirSeparator . implode($dirSeparator, $pathsArr);

Upvotes: 0

phihag
phihag

Reputation: 288100

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

Upvotes: 9

Allain Lalonde
Allain Lalonde

Reputation: 93408

This may do it.

function is_not_meta_dir($file_name) {
  // return true if $filename matches some pattern.
  // without knowing the format of your $dir_array
  return $file_name != '.' && $file_name != '..';
}

$new_dir_array = array_filter($dir_array, 'is_not_meta_dir');

Upvotes: 1

Jeff Winkworth
Jeff Winkworth

Reputation: 4996

no regex is needed, just unset() the first two values.

$d = dir($dir);
unset($d[0]);
unset($d[1]);

Upvotes: 1

ZombieSheep
ZombieSheep

Reputation: 29963

I'd do something like this (code may not work without effort since I haven't worked in PHP for years)

<?
if ($handle = opendir('/myPath')) 
  {
  while (false !== ($file = readdir($handle)))
  { 
    if (bool is_dir ( $file ) && substring($file,0,1) != ".")
      {
        $filesArray[] = $file; // this is the bit I'm not sure of the syntax for. 
      }
    }
  }
?>

EDIT misread the question - This should now add to the array all the folder names ion myPath that are not "." or ".."

Upvotes: 0

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179169

I would do it like this:

$items = array();
$handle = opendir('path/to/dir');
while (($item = readdir($handle)) !== false) {
    if (! in_array($item, array('.', '..'))) {
        $items[] = $item;
    }
}
closedir($handle);

print_r($items);

Although, I'd rather prefer DirectoryFilterDots but it's kind of rare in the available PHP distributions.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655499

You don’t need a regular expression to test this. Just use plain string comparison:

if ($dir_array[$i] == '.' || $dir_array[$i] == '..' || is_file($dir_array[$i])) {
    unset($dir_array[$i]);
}

Upvotes: 1

Related Questions