note 15589 rejected from function.readdir by sander
| From: | sander@php.net | Date: | Sat, 06 Apr 2002 19:16:22 +0000 |
| Subject: | note 15589 rejected from function.readdir by sander | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-28755@lists.php.net to get a copy of this message | ||
A concise listing of directories and files contained by a directory.
Directory names end with a / to dinstinguish them from file names.
Just for ease of further processing (e.g. directory mirroring).
Note:
when declaring variables, their type is indicated within /**/.
I think it's a useful way to document them.
function
listDir(
/*string*/$path, //directory path terminated by /
/*string[]*/&$files //array of directory and file names
){
/*int*/$dir = openDir($path);
while(/*string*/$item = readDir($dir)){
if(($item == ".") || ($item == "..")) continue; //skip endless looping
/*string*/$itempath = "$path$item";
if(is_dir($itempath)){
$itempath .= '/'; //trailing / for a directory
listDir($itempath, $files); //recursive call
}
$files[] = $itempath;
}
closedir($dir);
}
//Usage :
//$DirPath has been initialized with the directory path you want to list.
listDir($DirPath, $files=array());
for($i=0,$m=count($files);$i<$m;$i++) echo "$files[$i]<br>";