php recursive folder readdir vs find performance

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

readdir vs scandir

1] Which of the functions is faster? 2] what are the differences? Differences 1] readdir returns the name of the next entry in the directory. Scandir returns an array of files and directories from t

perl readdir() on network directory – scalar vs list context and network performance

We have a network share with a large number of files and are going to iterate over all files via perl readdir(). Which is more efficient with network communication (or are they the same)? 1) readdir i

PHP: readdir to scandir?

i wonder how i can transform exactly the following piece of code to scandir instead of readdir? $path = ‘files’; //shuffle files $count = 0; if ($handle = opendir($path)) { $retval = array(); while (f

readdir() includes an empty folder

I have this part of script that echos the folders in albums folder and arrange them by the alphabet, but for some reason it also includes an empty folder. $directory = opendir(albums/); $items = a

PHP: How to run readdir() as root

I am trying to make a lightweight frontend for my fileserver. I use PHP to display all the files, but readdir() is executed as the http user, because apache is started as this user. This is a problem,

Apache vs. IIS PHP performance comparison

Does anyone know of any links to realistic performance comparisons of IIS vs. Apache for PHP hosting? I am looking to utilise existing infrastructure for a change of technologies from .NET to a PHP ap

PHP abstract class vs interface (performance)

In PHP 5 and later what’s faster – an interface or abstract class ? abstract class MyPluginModel { vs interface MyPluginModel { Any ideas how they compare in terms of memory and time performance?

PHP Crawler – Why is readdir while loop quitting when it reaches empty folder?

I have the following function as part of a larger program that crawls the contents of a provided path, and indexes any .htm or .html pages that it finds in the parent folder or any subfolders. My craw

PHP readdir() Error 500 Endless Loop

I have a script that works on the server I pay for. However when i try to install the same code under localhost i get this error on an endless loop. PHP Warning: readdir() expects parameter 1 to be

Python recursive generators performance

In python, when changing a purely recursive function into a recursive generator (not a plain generator) the performance seems to be degrading. For example, here is a performance comparison between two

Answers

‘find’ is not portable, it’s a unix/linux command. readdir() is portable and will work on Windows or any other OS. Moreover, ‘find’ without any parameters is recursive, so if you’re in a dir with lots of subdirs and files, you will get to see all of them, rather than only the contents of that $path.

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.