get files in directory recursively [php]

Getting files from a directory can be useful if you want to access your files but forgot what subfolder names you put there (if you’ve turned indexing off, you can’t just look at the dir url and see an autogenerated list). What I do sometimes is make a php file with some wonky name, like “zebraBreath.php” and make one myself:

Method 1

/**
 * @param $path A directory
 * @param array $args Argument array:
 *      hidden: If true, hidden files beginning with a dot will be included
 *      ignoreRef: If true, . and .. are ignored
 *      recursive: If true, this function is recursive
 *      ignore: An array of paths to ignore
 * @return array Files in the directory
 */
public static function getFiles($path, $args = array()) {
    $hidden = self::set_default($args['hidden'], TRUE);
    $ignoreRef = self::set_default($args['ignoreRef'], TRUE);
    $recursive = self::set_default($args['recursive'], FALSE);
    $ignore = self::set_default($args['ignore'], NULL);

    $ignore_map = array();
    if ($ignore) {
        foreach ($ignore as $i) {
            if (is_dir($i)) {
                $i = CrayonUtil::path_slash($i);
            }
            $ignore_map[$i] = TRUE;
        }
    }

    $files = glob($path . '*', GLOB_MARK);
    if ($hidden) {
        $files = array_merge($files, glob($path . '.*', GLOB_MARK));
    }
    if ($ignoreRef || $ignore) {
        $result = array();
        for ($i = 0; $i < count($files); $i++) {
            $file = $files[$i];
            if (!isset($ignore_map[$file]) && (!$ignoreRef || (basename($file) != '.' && basename($file) != '..'))) {
                $result[] = $file;
                if ($recursive && is_dir($file)) {
                    $result = array_merge($result, self::getFiles($file, $args));
                }
            }
        }
    } else {
        $result = $files;
    }
    return $result;
}

Method 2

function getDirectory( $path = '.', $level = 0 ){

   $ignore = array( 'cgi-bin', '.', '..');
   // Directories to ignore when listing output. Many hosts
   // will deny PHP access to the cgi-bin.

   $dh = @opendir( $path );
   // Open the directory to the handle $dh

   while( false !== ( $file = readdir( $dh ) ) ){
   // Loop through the directory

       if( !in_array( $file, $ignore ) ){
       // Check that this file is not to be ignored

           $spaces = str_repeat( ' ', ( $level * 4 ) );
           // Just to add spacing to the list, to better
           // show the directory tree.

           if( is_dir( "$path/$file" ) ){
           // Its a directory, so we need to keep reading down...
              if($level==0){
                echo "$spaces $file
"; } else { echo "$spaces $file
"; } getDirectory( "$path/$file", ($level+1) ); // Re-call this same function but on a new directory. // this is what makes function recursive. } else { //echo "$spaces $file
"; // Just print out the filename } } } closedir( $dh ); // Close the directory handle }

Method 3

function corbDirectory($where) {
  $myDirectory = opendir($where);

  // get each entry
  while($entryName = readdir($myDirectory)) {
     if ($entryName != "." && $entryName != "..") {
        $dirArray[] = $entryName;
     }
  }
  // close directory
  closedir($myDirectory);

  //   count elements in array
  $indexCount   = count($dirArray);
  sort($dirArray);
 
  for($index=0; $index < $indexCount; $index++){
     $realname = basename("$dirArray[$index]", ".php");
     $realname = eregi_replace("_", " ", $realname);
     $realname = ucwords($realname);

     $pos = strpos($dirArray[$index], ".");
     if($pos===false) {

        print("$dirArray[$index]n
"); } else { print("$dirArray[$index]n
"); } } print("
"); }

Method 4

function scanIt($where) {
  $files1 = scandir($where);
  foreach ($files1 as $value) {
    echo $value . "
"; } }