Current location: Hot Scripts Forums » Programming Languages » PHP » Faster way to list dir ?


Faster way to list dir ?

Reply
  #1 (permalink)  
Old 11-29-08, 03:28 PM
peuplarchie's Avatar
peuplarchie peuplarchie is offline
Newbie Coder
 
Join Date: Nov 2008
Posts: 64
Thanks: 1
Thanked 0 Times in 0 Posts
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 !
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 11-29-08, 05:14 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
I think this is reasonably snappy in terms of performance:

PHP Code:

<?php

// Source: Bit Repository
// URL: http://www.bitrepository.com/

function read_whole_directory($dir){
if (
$handle opendir($dir)){
$array = array();

    while (
false !== ($file readdir($handle))) {
        if (
$file != "." && $file != "..") {

        if(
is_dir($dir.$file)){
                     
$array[][$file] = read_whole_directory($dir.$file.'/');
        }else{
                     
$array[] = $file;
        }
        }
    }
    
closedir($handle);
}

if(
is_array($array)){
    return 
$array;
    }else{
    return 
false;
    }
}

$dir '/your/path/to/public_html/'// IMPORTANT: with '/' at the end

$directory read_whole_directory($dir);

echo 
"<pre>"print_r($directory); echo "</pre>";
?>
NOTE: Make sure your directory string has ‘/’ at the end.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 11-29-08, 08:35 PM
Mike Mike is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$files glob('folder_name/*.*');

echo(
'<pre>');
print_r($files);
echo(
'</pre>'); 
Just replace 'folder_name' with your folder.

http://us.php.net/glob
__________________
Mike
ImperialBB Development Coordinator
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 11-29-08, 08:39 PM
peuplarchie's Avatar
peuplarchie peuplarchie is offline
Newbie Coder
 
Join Date: Nov 2008
Posts: 64
Thanks: 1
Thanked 0 Times in 0 Posts
Thanks Mike and End User,
Mike how would I put my code that do a different thing when its a folder then when its a file ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 11-29-08, 09:46 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by Mike View Post
PHP Code:

$files glob('folder_name/*.*');
echo(
'<pre>');
print_r($files);
echo(
'</pre>'); 
This code will return the contents of a single level, but it won't recursively list subfolders and the files in them (which is what the OP wanted).
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 11-29-08, 11:23 PM
Mike Mike is offline
Newbie Coder
 
Join Date: Aug 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
My apologies then. I misread it then. In that case... End User's solution would be the best route.
__________________
Mike
ImperialBB Development Coordinator
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 12-02-08, 03:27 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Another approach would be to use Apache's mod_autoindex.

http://httpd.apache.org/docs/2.0/mod/mod_autoindex.html

Notes about autoindex.

http://know-waste.com/2008/11/26/pol...s-without-serv
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] dir list filtering extensions id10tn00b PHP 4 08-06-08 01:01 AM
List files in dir witakr PHP 6 04-25-07 02:26 PM


All times are GMT -5. The time now is 08:55 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.