PHP directory iteration

From Ferrous Moon Research

Jump to: navigation, search
This article was written by Prophile.
This document is classified ICARS::0, and is for public inspection.

There are several methods of iteration through the contents of a directory, and there are arguments for using each. These methods are scandir(), opendir() and closedir(), SPL, or `ls`.

scandir() returns an array of strings representing items in a directory. For example

 function list_directory ( $directory )
 {
     return scandir($directory);
 }

list_directory would return an array of strings.


opendir() and co are the PHP 4 equivalent of scandir(), and are used as follows:

function list_directory ( $directory )
{
    $scratchpad = array();
    $iter = opendir($directory);
    while ($file = readdir($iter))
        $scratchpad[] = $file;
    closedir($iter);
    return $scratchpad;
}


SPL is the Object-Oriented way of achieving this.

function list_directory ( $directory )
{
    $scratchpad = array();
    $iter = new DirectoryIterator ( $directory );
    while ($file = $iter->getFilename())
    {
        $scratchpad[] = $file;
        $iter->next();
    }
    return $scratchpad;
}

Finally, this could be achieved using the command-line utility ls:

function list_directory ( $directory )
{
    return explode("\n", `ls -1 $directory`);
}

Which uses a very reliable command-line utillity, but adds the overheads of shell processing.

I benchmarked all of these by running them first 100 times, then 10000 times.

The results are as follows:

Test with 100 iterations-
scandir took 0.28 seconds
opendir took 0.47 seconds
SPL took 0.51 seconds
ls took 3.29 seconds (!)
scandir was the obvious winner.
Test with 10000 iterations-
scandir took 26.6 seconds
opendir took 27.88 seconds (notice this is a much less dramatic increase on scandir)
SPL took 39.58 seconds (and suddenly this is much slower)
ls took over 5 minutes, tripping the PHP time limit system (!!!)

So scandir is the fastest, opendir coming a close second, SPL a less close third and finally evidence that ls is a bad idea.

Personal tools