What is the issue with reading all files from a folder and its subfolders in PHP?

The issue with reading all files from a folder and its subfolders in PHP is that it can be resource-intensive and slow, especially if there are a large number of files. One way to solve this issue is by using RecursiveDirectoryIterator and RecursiveIteratorIterator classes in PHP, which allow you to recursively iterate over all files in a directory and its subdirectories efficiently.

$directory = 'path/to/directory';

$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS),
    RecursiveIteratorIterator::SELF_FIRST
);

foreach ($files as $file) {
    if ($file->isFile()) {
        echo $file->getPathname() . PHP_EOL;
    }
}