Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6901705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:42:24+00:00 2026-05-27T07:42:24+00:00

i came across few articles about performance and readdir here is the php script:

  • 0

i came across few articles about performance and readdir
here is the php script:

function getDirectory( $path = '.', $level = 0 ) { 
    $ignore = array( 'cgi-bin', '.', '..' );
    $dh = @opendir( $path );
    while( false !== ( $file = readdir( $dh ) ) ){
        if( !in_array( $file, $ignore ) ){
            $spaces = str_repeat( ' ', ( $level * 4 ) );
            if( is_dir( "$path/$file" ) ){
                echo "$spaces $file\n";
                getDirectory( "$path/$file", ($level+1) );
            } else {
                echo "$spaces $file\n";
            }
        }
    }
    closedir( $dh );
}
getDirectory( "." );  

this echo the files/ folders correctly.

now i found this:

$t = system('find');
print_r($t);

which also find all the folders and files then i can create an array like the first code.

i think the system('find'); is faster than the readdir but i want to know if it’S a good practice?
thank you very much

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:42:25+00:00Added an answer on May 27, 2026 at 7:42 am

    Here’s my benchmark using a simple for loop with 10 iteration on my server:

    $path = '/home/clad/benchmark/';
    // this folder has 10 main directories and each folder as 220 files in each from 1kn to 1mb
    
    // glob no_sort = 0.004 seconds but NO recursion
    $files = glob($path . '/*', GLOB_NOSORT);
    
    // 1.8 seconds - not recommended
    exec('find ' . $path, $t);
    unset($t);
    
    // 0.003 seconds
    if ($handle = opendir('.')) {
     while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != "..") {
       // action
      }
     }
     closedir($handle);
    }
    
    // 1.1 seconds to execute
    $path = realpath($path);
    $objects = new RecursiveIteratorIterator(
     new RecursiveDirectoryIterator($path), RecursiveIteratorIterator::SELF_FIRST);
      foreach($objects as $name => $object) {
       // action
      }
    }
    

    Clearly the readdir is faster to use specially if you have lots of traffic on your site.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just came across few articles, while selecting a wiki for my personal site.
I came across a few articles like this one , which suggest that some
I came across a few articles referring to a C++ blob. What this is?
I'm a C programmer came across this Delphi function and have a few questions.
I would like to add authentication to my Rails app. I came across few
I came across a competitor product to VisualSVN a few days back. It provides
I just came across this code and a few Google searches turn up no
A few years ago I came across a T-SQL technique to validate a stored
I came across a strange behavior while theming Drupal. I turned a few modules
Few months ago, I have came across a Silverlight tutorial of Eric Cartman (South

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.