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
- <?php
- /*
- *
- * Directory File Search Class
- *
- * Class to search a directory for files.
- *
- * Written By Jacob Wyke - jacob@redvodkajelly.com - www.redvodkajelly.com - and Jon Guymer.
- *
- * LICENSE
- * ——-
- * 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.
- *
- * USAGE
- * —–
- *
- * Call the class with the required parameters and the results of the search will be stored in $arrFilesFound.
- *
- * $objDirectorySearch = new RVJ_DirectorySearch(’C:\myFolder’, array(’txt’, ‘doc’));
- *
- * This will then search all files and folders within "C:\myFolder\" for any .txt or .doc files and return the results in $arrFilesFound.
- *
- * print_r($objDirectorySearch->arrFilesFound);
- *
- * To only search the selected folder and NOT traverse to other folders add the third parameter.
- *
- * $objDirectorySearch = new RVJ_Directory(’C:\myFolder’, array(’txt’, ‘doc’), 0);
- *
- * If you are running this script on Unix/Linux/OS X then make sure you change the $strPathDivider to ‘/’ on line 39.
- */
- class RVJ_DirectorySearch {
- var $strDirectory;
- var $strCurrentDirectory;
- var $arrFileTypes = array();
- var $boolTraverse = 1;
- var $arrFilesFound = array();
- var $strPathDivider = "\\";
- /*
- *
- * @Method: RVJ_DirectorySearch
- * @Parameters: 3
- * @Param-1: strDirectory - String - The directory path
- * @Param-2: arrFileTypes - Array - An array of file types to find
- * @Param-3: boolTraverse - Bool - If you want to traverse directorys to look for files deeper in the file structure
- * @Description: Finds files within a directory
- *
- */
- function RVJ_DirectorySearch($strDirectory, $arrFileTypes = ‘0′, $boolTraverse = ‘1′){
- $this->strDirectory = $strDirectory;
- $this->arrFileTypes = $arrFileTypes;
- $this->boolTraverse = $boolTraverse;
- $this->getDirectoryContent($this->strDirectory);
- }
- /*
- *
- * @Method: getDirectoryContent
- * @Parameters: 1
- * @Param-1: strDirectory - String - The directory path
- * @Description: Gets the content of a passed directory path
- *
- */
- function getDirectoryContent($strDirectory){
- //opens the directory up
- if($resHandle = opendir($strDirectory)){
- //reads the directories content
- while (false !== ($strFile = readdir($resHandle))){
- if($strFile <> ‘.’ AND $strFile <> ‘..’){
- $strFullFilePath = $strDirectory.$this->strPathDivider.$strFile;
- if($this->boolTraverse AND is_dir($strFullFilePath)){
- $this->getDirectoryContent($strFullFilePath);
- }else{
- if($this->arrFileTypes){
- $arrFileParts = explode(".", $strFile);
- $arrFileParts = array_reverse($arrFileParts);
- $strFileExt = $arrFileParts[0];
- if(in_array($strFileExt, $this->arrFileTypes)){
- $this->arrFilesFound[] = $strFullFilePath;
- }
- }else{
- $this->arrFilesFound[] = $strFullFilePath;
- }
- }
- }
- }
- closedir($resHandle);
- }
- }
- }
- ?>



