Find a file or folder within a directory. Say we want will loop n times by all subdirectories of a root directory and find a particular folder or file and know your address.
In my case I needed to find each existing file in a directory metadata.opf a Calibre library and take all the data for each document this, if we consider that this directory had 20,000 folders author is a task that is humanly impossible . This recursive algorithm achieves do my homework. I hope it will help you.
<?php
$root = '../Classes';
$search_parameter = "CachedObjectStorageFactory.php";
spider($root, $search_parameter);
closedir();
function spider($dir,$fileName=""){
$handle = opendir($dir);
while($file= readdir($handle)){
if($file != "." && $file != ".."){
if($fileName=="")
echo $dir."/".$file."<br>";
else
if($file == $fileName)
echo $dir."/".$file."<br>";
if(!is_file($dir."/".$file))
spider($dir."/".$file,$fileName);
}
}
}
?>