Faster way to list dir ?

Discussion in 'Programming' started by Peuplarchie, Nov 29, 2008.

  1. #1
    Good day to you all,
    I have a piece of code which read a directory and return a list of file and folders that is with in recursively.

    This code is very slow.
    Maybe you can help me make it go faster ?


    <!doctype html public "-//w3c//dtd html 3.2//en">
    
    
    <?php
    
      function CountDir($aDir, $aRecurse)
      {
        $Count = 0;
    
        $d = dir($aDir);
    
        while ($Entry = $d->Read())
        {
          if (!(($Entry == "..") || ($Entry == ".")))
          {
            if (Is_Dir($aDir . '/' . $Entry))
            {
              if ($aRecurse)
              {
                $Count += CountDir($aDir . '/' . $Entry, $aRecurse);
              }
            }
            else
            {
              $Count++;
            }
          }
        }
        
        return $Count;
      }
    
    
    
    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( '&nbsp;', ( $level * 4 ) );
                // Just to add spacing to the list, to better
                // show the directory tree.
                
                $rest = substr($file, 0, -4);
    
                if( is_dir( "$path/$file" ) ){
                // Its a directory, so we need to keep reading down...
    
    echo '<tr><td align="left"><a href="'.$path.'/'.$file.'" align="left" class="white00" target="image">'.$spaces.'<img src="folder_icon.gif" border="0"> '.$file.'</a> - '.CountDir($path.'/'.$file, False).' </td></tr>';
    
    
    
    
    
                    getDirectory( "$path/$file", ($level+1) );
                    // Re-call this same function but on a new directory.
                    // this is what makes function recursive.
                
                } else {
                
    
    
                    // Just print out the filename
                
                }
            }
        }
        closedir( $dh );
        // Close the directory handle
    }
    
    getDirectory( "." );
    // Get contents of the "files/includes" folder  
    
    ?>
    
    PHP:
    Thanks !
     
    Peuplarchie, Nov 29, 2008 IP
  2. misbah

    misbah Active Member

    Messages:
    265
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    68
    #2
    your script is good, I think faster way for your script is upgrade your server processor
    or remove the filter statemen like ignore file
     if( !in_array( $file, $ignore ) )
    PHP:
    and this line
    $spaces = str_repeat( '&nbsp;', ( $level * 4 ) );
    $rest = substr($file, 0, -4);
    PHP:
    or you can remove this line too (if you don't want count your file)
     - '.CountDir($path.'/'.$file, False).' 
    PHP:
    and if you want show only one layer list, you can remove this lines
                    getDirectory( "$path/$file", ($level+1) );
                    // Re-call this same function but on a new directory.
                    // this is what makes function recursive.
    
    PHP:
    finally, less filter, less resource, and less output/design
     
    misbah, Nov 30, 2008 IP