File_Find::mapTreeMultiple PHP Method

mapTreeMultiple() public method

Map the directory tree given by the directory parameter.
Author: Mika Tuupola ([email protected])
public mapTreeMultiple ( string $directory, integer $maxrecursion, $count ) : array
$directory string contains the directory path that you want to map.
$maxrecursion integer maximun number of folders to recursive map
return array a multidimensional array containing all subdirectories and their files. For example: Array ( [0] => file_1.php [1] => file_2.php [subdirname] => Array ( [0] => file_1.php ) )
    function &mapTreeMultiple($directory, $maxrecursion = 0, $count = 0)
    {
        $retval = array();
        $count++;
        /* strip trailing slashes */
        $directory = preg_replace('![\\\\/]+$!', '', $directory);
        if (is_readable($directory)) {
            $dh = opendir($directory);
            while (false !== ($entry = @readdir($dh))) {
                if ($entry != '.' && $entry != '..') {
                    array_push($retval, $entry);
                }
            }
            closedir($dh);
        }
        sort($retval);
        while (list($key, $val) = each($retval)) {
            $path = $directory . "/" . $val;
            if (!is_array($val) && is_dir($path)) {
                unset($retval[$key]);
                if ($maxrecursion == 0 || $count < $maxrecursion) {
                    $retval[$val] =& File_Find::mapTreeMultiple($path, $maxrecursion, $count);
                }
            }
        }
        return $retval;
    }

Usage Example

Example #1
0
 /**
  * Map the directory tree given by the directory parameter.
  *
  * @param string $directory contains the directory path that you
  * want to map.
  * @param integer $maxrecursion maximun number of folders to recursive 
  * map
  *
  * @return array a multidimensional array containing all subdirectories
  * and their files. For example:
  *
  * Array
  * (
  *    [0] => file_1.php
  *    [1] => file_2.php
  *    [subdirname] => Array
  *       (
  *          [0] => file_1.php
  *       )
  * )
  *
  * @author Mika Tuupola <*****@*****.**>
  * @access public
  * @static
  */
 function &mapTreeMultiple($directory, $maxrecursion = 0, $count = 0)
 {
     $retval = array();
     $count++;
     /* strip trailing slashes */
     $directory = preg_replace('![\\\\/]+$!', '', $directory);
     if (is_readable($directory)) {
         $dh = opendir($directory);
         while (false !== ($entry = @readdir($dh))) {
             if ($entry != '.' && $entry != '..') {
                 array_push($retval, $entry);
             }
         }
         closedir($dh);
     }
     sort($retval);
     while (list($key, $val) = each($retval)) {
         if (!is_array($val)) {
             $path = $directory . "/" . $val;
             if (is_dir($path)) {
                 unset($retval[$key]);
                 if ($maxrecursion == 0 || $count < $maxrecursion) {
                     $retval[$val] =& File_Find::mapTreeMultiple($path, $maxrecursion, $count);
                 }
             }
         }
     }
     return $retval;
 }
All Usage Examples Of File_Find::mapTreeMultiple