#native_company# #native_desc#
#native_cta#

Simple Recursive Directory Search

By Jayson Wallace
on September 4, 2001

Version: 1.0a

Type: Function

Category: File Management

License: GNU General Public License

Description: This function can be used as a simple search. As it is now, you can use a file extension (ex. “.php”) to find all the files in the root directory and below that match the extension. It returns a list of file names in the format “/new_paths_if_any/file_name”.

<?php
/* Directory to originate from: */
$root_directory = ".";

/* Folder to originate from, if left blank then will recurse through entire tree: */
$path_extension = "";

/* File extension to search for, if left blank then will show all files of all types, example equivelant to "*.php" */
$check_extension = ".php";

function recursive_dir($root,$path_ext = "",$check_ext = "",$new_path_ext = "") {
  $dh = opendir($root.$path_ext.$new_path_ext);
  echo "<br>Handle: ".$dh."<br>n";
  echo "Path: ".$root.$path_ext.$new_path_ext."<br>n";
  while(false !== ($entry=readdir($dh))) {
    if($entry != "." && $entry != ".." && is_dir($root.$path_ext.$new_path_ext."/".$entry)) {
      $prev_path_ext = $new_path_ext;
      $new_path_ext .= "/".$entry;
      recursive_dir($root,$path_ext,$check_ext,$new_path_ext);
      echo "Closing Path: ".$root.$path_ext.$new_path_ext."<br>n";
      $new_path_ext = $prev_path_ext;
    }  
    elseif($entry != "." && $entry != ".." && eregi("($check_ext)$",$entry)) {
      echo $new_path_ext."/".$entry."<br>n";
    }
  }
  closedir($dh);
}

recursive_dir($root_directory,$path_extension,$check_extension);
?>