note 40892 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 19:05:36 +0000 |
| Subject: | note 40892 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126543@lists.php.net to get a copy of this message | ||
Note Submitter: gehring[at-no-2-spam]egotec.com
----
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