Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post

    Post Faster way to list dir ?

    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 ?


    PHP Code:
    <!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 ){




        
    $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 ) );
                
    // Just to add spacing to the list, to better
                // show the directory tree.
                
                
    $rest substr($file0, -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.'/'.$fileFalse).' </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  

    ?>
    Thanks !

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    What version of PHP are you using?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Regular Coder
    Join Date
    Feb 2006
    Posts
    262
    Thanks
    23
    Thanked 1 Time in 1 Post
    php version 4


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •