What are potential pitfalls when using SPLIterators in PHP for directory scanning?

One potential pitfall when using SPLIterators for directory scanning in PHP is that the iterator may not handle large directories efficiently, leading to performance issues. To solve this, you can limit the number of files being scanned at a time by using a RecursiveIteratorIterator with a RecursiveDirectoryIterator.

$dir = new RecursiveDirectoryIterator('/path/to/directory');
$iterator = new RecursiveIteratorIterator($dir, RecursiveIteratorIterator::SELF_FIRST);

foreach ($iterator as $file) {
    echo $file->getPathname() . PHP_EOL;
}