note 40892 modified in function.readdir by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 27 Jun 2004 13:11:51 +0000 |
| Subject: | note 40892 modified in function.readdir by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-71878@lists.php.net to get a copy of this message | ||
If you want to walk through a directory and want to use a callback function for every dir or file
you get you can use this
its like the perl File::Find method "find"
<?php
function find($callback, $dir=Array())
{
foreach($dir as $sPath) {
if ($handle = opendir($sPath)) {
while (false !== ($file = readdir($handle))) {
if($file !== "." && $file !== "..") {
if(is_dir($sPath."/".$file)) {
eval("$callback(\"$sPath\", \"$file\");");
find($callback, Array($sPath."/".$file));
} else {
eval("$callback(\"$sPath\", \"$file\");");
}
}
}
closedir($handle);
}
}
}
function walk($dir, $file)
{
print ">>> $dir/$file\n";
}
find("walk", Array("/etc"));
?>
hope it helps
jg
--was--
If you want to walk through a directory and want to use a callback function for every dir or file
you get you can use this
its like the perl File::Find method "find"
<?php
function find($callback, $dir=Array())
{
foreach($dir as $sPath) {
if ($handle = opendir($sPath)) {
$append = false;
while (false !== ($file = readdir($handle))) {
if($file !== "." or $file !== "..") {
if(is_dir($sPath."/".$file)) {
eval("$callback(\"$sPath\", \"$file\");");
find($callback, Array($sPath."/".$file));
} else {
eval("$callback(\"$sPath\", \"$file\");");
}
}
}
closedir($handle);
}
}
}
function walk($dir, $file)
{
print ">>> $dir/$file\n";
}
find("walk", Array("/etc"));
?>
hope it helps
jg
http://php.net/manual/en/function.readdir.php