note 41416 deleted from function.scandir by aidan
| From: | aidan@php.net | Date: | Sat, 11 Sep 2004 03:22:06 +0000 |
| Subject: | note 41416 deleted from function.scandir by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76436@lists.php.net to get a copy of this message | ||
Note Submitter: csaba at alum dot mit dot edu
----
For when you only want to list so may levels of subdirectories...
<?
function dirList($path="", $types=2, $levels=1) {
// returns an array of the specified files/directories
// $path defaults to the current working directory
// return $types: 2 => files; 1 => directories; 3 => both;
// $levels: 1 => look in the $path only; 2 => $path and all children;
// 3 => $path, children, grandchildren; 0 => $path and all subdirectories;
// less than 0 => complement of -$levels, OR everything starting -$levels down
// e.g. -1 => everthing except $path; -2 => all descendants except $path +
children
// Note that directories are prefixed with a '*'
// That can be eliminated from the line above the return
$pathSep = strstr(PHP_OS, "WIN") ? "\\" : "/";
if (!@$path) $path = getcwd();
$aRes = array();
$aAcc = array();
$aDir = array(realpath($path));
for ($i=$levels>0?$levels++:-1;$i--&&$aDir;$aDir=$aAcc,$aAcc=array())
while ($dir = array_shift($aDir))
foreach (scandir ($dir) as $fileOrDir)
if ($fileOrDir!="." && $fileOrDir!="..") {
if ($dirP = is_dir ($rp="$dir$pathSep$fileOrDir")) $aAcc[] = $rp;
if ($i<$levels-1 && ($types & (2-$dirP))) $aRes[] =
($dirP?"*":"") . $rp; }
return $aRes;
}
?>