Re: directory help
| From: | Joe Conway | Date: | Mon, 17 Jul 2000 05:31:45 +0000 |
| Subject: | Re: directory help | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-6872@lists.php.net to get a copy of this message | ||
>>Hi,
>> I need to recursivly traverse a directory path, anyone have any
>>ideas on how to do this? I've tried many things, but none have been
>>successful so far. Thanks!
>>
>>Jay
>Here's a sample of what I usually use for this purpose:
Here's a better version. I found a few bugs in my first one :)
Joe
<?
function recursedirs($currentdir,$level) {
$newlevel = $level + 1;
if ($newlevel > 99) {
print "Too many levels!<br>\r\n";
exit;
}
if (substr(strrev($currentdir),0,1) != "/") {
$currentdir = $currentdir . "/";
};
print "Entering " . $currentdir . "<br>\r\n";
$retval = @chdir($currentdir);
if ($retval != 1) {
return $currentdir . " skipped!";
};
$dhdl=opendir(".");
while ($filename = readdir($dhdl)) {
if(is_dir($filename)) {
$dlist[] = $filename;
}
if(is_file($filename)) {
$flist[] = $filename;
}
}
closedir($dhdl);
if($flist) {
while (list ($key, $filename) = each ($flist)) {
print $level . ": " . $currentdir . $filename . "<br>\r\n";
};
};
if($dlist) {
while (list ($key, $filename) = each ($dlist)) {
if (!($filename == "." || $filename == "..")) {
$status = recursedirs($currentdir . $filename,$newlevel);
if ($status) {
print "<b><font color=red>" . $status .
"</font></b><br>\r\n";
}
};
};
};
print "Exiting " . $currentdir . "<br>\r\n";
return;
}
//$rootdir = "/home/httpd/html/dev";
$rootdir = "/etc";
$level = 0;
print "<html><body>\r\n";
recursedirs($rootdir,$level);
print "</body></html>";
?>
>$rootdir is where the traversal starts. The $level check prevents going too deep.
>Hope this helps!
>Joe
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
For additional commands, e-mail: php-general-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net