RedVodkaJelly Logo

PHP Directory Search Class

POSTED AT 15:34 on 23rd May 2008

This is a PHP class that deals with finding different file types within directories.

You simply pass it the directory you want to search within and an array of file types your looking for and the script will return with an array of matching files. It traverses child directories by default and so you could search your whole system for a certain file type etc.

Source Code

  1. <?php 
  2. /* 
  3. * Directory File Search Class 
  4. * Class to search a directory for files. 
  5. * Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com - and Jon Guymer. 
  6. * LICENSE 
  7. * ——- 
  8. * Feel free to use this as you wish, just give me credit where credits due and drop me an email telling me what your using it for so I can check out all the cool ways its been used. 
  9. * USAGE 
  10. * —– 
  11. * Call the class with the required parameters and the results of the search will be stored in $arrFilesFound. 
  12. *   $objDirectorySearch = new RVJ_DirectorySearch(’C:\myFolder’, array(’txt’, ‘doc’)); 
  13. * This will then search all files and folders within "C:\myFolder\" for any .txt or .doc files and return the results in $arrFilesFound. 
  14. *   print_r($objDirectorySearch->arrFilesFound); 
  15. * To only search the selected folder and NOT traverse to other folders add the third parameter. 
  16. *   $objDirectorySearch = new RVJ_Directory(’C:\myFolder’, array(’txt’, ‘doc’), 0); 
  17. * If you are running this script on Unix/Linux/OS X then make sure you change the $strPathDivider to ‘/’ on line 39. 
  18. */ 
  19.  
  20. class RVJ_DirectorySearch { 
  21.     
  22.    var $strDirectory; 
  23.    var $strCurrentDirectory; 
  24.    var $arrFileTypes = array(); 
  25.    var $boolTraverse = 1; 
  26.    var $arrFilesFound = array(); 
  27.    var $strPathDivider = "\\"; 
  28.  
  29.    /* 
  30.    * 
  31.    *   @Method:      RVJ_DirectorySearch 
  32.    *   @Parameters:   3 
  33.    *   @Param-1:      strDirectory - String - The directory path 
  34.    *   @Param-2:      arrFileTypes - Array - An array of file types to find 
  35.    *   @Param-3:      boolTraverse - Bool - If you want to traverse directorys to look for files deeper in the file structure 
  36.    *   @Description:   Finds files within a directory 
  37.    * 
  38.    */    
  39.    function RVJ_DirectorySearch($strDirectory, $arrFileTypes = ‘0′, $boolTraverse = ‘1′){ 
  40.       $this->strDirectory = $strDirectory; 
  41.       $this->arrFileTypes = $arrFileTypes; 
  42.       $this->boolTraverse = $boolTraverse; 
  43.        
  44.       $this->getDirectoryContent($this->strDirectory);       
  45.    } 
  46.     
  47.    /* 
  48.    * 
  49.    *   @Method:      getDirectoryContent 
  50.    *   @Parameters:   1 
  51.    *   @Param-1:      strDirectory - String - The directory path 
  52.    *   @Description:   Gets the content of a passed directory path 
  53.    * 
  54.    */ 
  55.    function getDirectoryContent($strDirectory){       
  56.       //opens the directory up 
  57.       if($resHandle = opendir($strDirectory)){ 
  58.          //reads the directories content 
  59.          while (false !== ($strFile = readdir($resHandle))){  
  60.             if($strFile <> ‘.’ AND $strFile <> ‘..’){           
  61.                $strFullFilePath = $strDirectory.$this->strPathDivider.$strFile; 
  62.                  
  63.                if($this->boolTraverse AND is_dir($strFullFilePath)){                   
  64.                   $this->getDirectoryContent($strFullFilePath); 
  65.                }else{                 
  66.                   if($this->arrFileTypes){                   
  67.                      $arrFileParts = explode(".", $strFile); 
  68.                      $arrFileParts = array_reverse($arrFileParts); 
  69.                      $strFileExt = $arrFileParts[0]; 
  70.                       
  71.                      if(in_array($strFileExt, $this->arrFileTypes)){ 
  72.                         $this->arrFilesFound[] = $strFullFilePath; 
  73.                      }                   
  74.                   }else{ 
  75.                      $this->arrFilesFound[] = $strFullFilePath; 
  76.                   }                   
  77.                }                
  78.             }     
  79.          } 
  80.          closedir($resHandle);  
  81.       } 
  82.    } 
  83.     
  84.  
  85. ?>