How to use DirectoryIterator effectively?

I need to filter some files from a directory containing many files. During my script, this function is called many times.

function getFilteredFiles($criteria) {
    static $files = '';           
    if ($files == '') {
        $files = new DirectoryIterator($path);
    }
    else {
        $files->rewind();
    }

    foreach($files as $file) {
         if (! $file->isDot()) {
             if (!$file->isDir()) {
                  //using $criteria
                  ...
              }
         }
    }
    ...
 }

Put DirectoryIterator in a static variable to allow php to go to the file system only once to get the files? (= Does php only work on the file system during the __ DirectoryIterator construct?)

+3
source share
2 answers

I looked at the source code and it looks like they are trying to rewind the stream that they read in the directory entries when you rewind the iterator. I dare not delve into the php streams.c file to look further, but based on the search for the stream that I saw, I would assume that it no longer gets into the file system.

, , strace.

+1

DirectoryIterator , php , ? (= php __ DirectoryIterator?)

.

?

+1

All Articles