TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-29-2008, 08:29 PM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application 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 !
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 11-29-2008, 09:04 PM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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.
__________________

Village Idiot is offline  
Reply With Quote
Old 11-29-2008, 11:53 PM   #3 (permalink)
The Addict
 
Enfernikus's Avatar
 
Join Date: Jun 2008
Posts: 335
Thanks: 2
Enfernikus is on a distinguished road
Default

You can try the DirectoryIterator class or the Glob() function to see if they speed anything up.
Enfernikus is offline  
Reply With Quote
Old 11-30-2008, 12:51 AM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Peuplarchie View Post
This code is very slow.
How slow is slow? How many files/folders are you recursing through (multiple times, looking at your functions)?
Salathe is offline  
Reply With Quote
Old 12-02-2008, 03:58 AM   #5 (permalink)
The Visitor
 
Join Date: Nov 2008
Posts: 3
Thanks: 0
liveordie is on a distinguished road
Default

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.
liveordie is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Status List codefreek TalkPHP Developer Team 12 11-24-2008 12:23 AM
List only some extention (File Listing) Peuplarchie Advanced PHP Programming 3 11-10-2008 01:14 PM
Need alphabetized directory list benton General 9 10-13-2008 06:34 PM
list populating proplem Peuplarchie Absolute Beginners 7 06-09-2008 09:02 AM
Problem with aligning list benton XHTML, HTML, CSS 18 04-11-2008 06:33 PM


All times are GMT. The time now is 05:13 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design