TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Faster way to list dir ? (http://www.talkphp.com/advanced-php-programming/3687-faster-way-list-dir.html)

Peuplarchie 11-29-2008 08:29 PM

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 !

Village Idiot 11-29-2008 09:04 PM

The only way I can immediately identify to optimize it is to hard code the ignore list opposed to reading an array every time, but that advantage will probably be negligible at best. It takes PHP a long time to do these things, that might just be a scripting language thing.

This is why I hate shared hosting, you do not have the ability to make programs for the server to run that execute more tedious tasks like the one you have here. I wrote a VB application to do something fairly similar and it could do very fast (an entire hard drive in <2min).

edit: Try appending your output to a single string then outputting it all at the end. You might be surprised how long it takes it to continuously output text.

Enfernikus 11-29-2008 11:53 PM

You can try the DirectoryIterator class or the Glob() function to see if they speed anything up.

Salathe 11-30-2008 12:51 AM

Quote:

Originally Posted by Peuplarchie (Post 20066)
This code is very slow.

How slow is slow? How many files/folders are you recursing through (multiple times, looking at your functions)?

liveordie 12-02-2008 03:58 AM

Have you tried the php5 SPL library?

PHP Code:

<?php
$path 
realpath('C:/xampp/htdocs/');

$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
foreach(
$objects as $object){
    echo 
$object->getFilename() . '<br />';
}

Something like that will recursively read through all of the directories and files. Its taken straight from the php manual for the most part. $object in the foreach loop is an instance of SplFileInfo

http://us3.php.net/manual/en/class.r...ryiterator.php

Just running some microtime tests on it, it will parse through 380 directories containing 3700 files in a little over 2 seconds.


All times are GMT. The time now is 10:44 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0