What exactly are the benefits of using a PHP 5 DirectoryIterator over PHP 4 “opendir/readdir/closedir”?

前端 未结 4 1295
迷失自我
迷失自我 2021-02-04 09:03

What exactly are the benefits of using a PHP 5 DirectoryIterator

$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) 
{
    // handle wh         


        
4条回答
  •  遥遥无期
    2021-02-04 09:55

    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.

提交回复
热议问题