$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/directory')); foreach ($iterator as $fileinfo) { if (!$fileinfo->isFile()) { continue; } echo $fileinfo->getFilename() . ' - ' . $iterator->getSubPath() . "\n"; }
$iterator = new RecursiveFilterIterator(new RecursiveDirectoryIterator('/path/to/directory')); $iterator = new RecursiveIteratorIterator($iterator); foreach ($iterator as $fileinfo) { echo $fileinfo->getFilename() . ' - ' . $iterator->getSubPath() . "\n"; }This code uses `RecursiveFilterIterator` to filter files based on their extension and then uses `RecursiveIteratorIterator` to iterate over the filtered files. The sub-path of each file is also printed. The `RecursiveIteratorIterator` class is part of the core PHP library and does not require any additional package.