note 41416 added to function.scandir
| From: | csaba at alum dot mit dot edu | Date: | Fri, 09 Apr 2004 01:40:40 +0000 |
| Subject: | note 41416 added to function.scandir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-67855@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.scandir.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+41416
Delete -- http://master.php.net/manage/user-notes.php?action=delete+41416&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+41416&report=yes
Search -- http://master.php.net/manage/user-notes.php