Quick Search:
 
 PHP Code: Recursive Directory Browser Jump to:  
Category: >> PHP Code >> Recursive Directory Browser  

<< lastnext >>

Snippet Name: Recursive Directory Browser

Description: This function takes in a directory path and recursively searches through the directory structure and returns all files in those directories. The file list is returned in an array.

This function has 1 required parameter and 2 optional parameters.

directoryToArray(Path to Directory [,file extention [,Display Full Path]]

If the file extension parameter is set, only files matching that extension will be displayed, ie, "jpg".

If the Display Full Path parameter is true, then the full path of the file will be returned. If it is false, only the file name will be returned. By default, this parameter is set to true.

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

<?PHP
 
FUNCTION directoryToArray($directory, $extension="", $full_path = TRUE) {
     $array_items = ARRAY();
     IF ($handle = OPENDIR($directory)) {
          WHILE (FALSE !== ($file = READDIR($handle))) {
               IF ($file != "." && $file != "..") {
                    IF (IS_DIR($directory. "/" . $file)) {
                         $array_items = ARRAY_MERGE($array_items, directoryToArray($directory. "/" . $file, $extension, $full_path)); 
                    }
                    ELSE { 
                         IF(!$extension || (EREG("." . $extension, $file)))
                         {
                              IF($full_path) {
                                   $array_items[] = $directory . "/" . $file;
                              }
                              ELSE {
                                   $array_items[] = $file;
                              }
                         }
                    }
               }
          }
          CLOSEDIR($handle);
     }
     RETURN $array_items;
}
 
?>


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org