update page now
Longhorn PHP 2026 - Call For Papers

Voting

: nine minus two?
(Example: nine)

The Note You're Voting On

sergio dot barrios at upr dot edu dot cu
9 years ago
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";

//if we call the function spider as spider($root); 
//will show all the directory content including subdirectories

//if we call the function spider as spider('../Classes', 'Shared');
//and will show the address of the directory

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);
         
      }    
   }
   
}     

?>

<< Back to user notes page

To Top