What exactly are the benefits of using a PHP 5 DirectoryIterator
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo)
{
// handle wh
A DirectoryIterator
provides you with items that make sense in themselves. For example, DirectoryIterator::getPathname() will return all the information that you need to access the file contents.
The information that readdir() provides to you only make sense locally, namely in combination with the parameter that you passed to opendir().
The DirectoryIterator
is implemented in terms of wrappers around the php_stream_*
functions, so no fundamentally different performance characteristics are to be expected. Particularly, items from the directory are read only when they are requested. Details can be found in the file
ext/spl/spl_directory.c
of the PHP source code.