$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/directory')); foreach ($files as $file) { if ($files->isFile()) { echo $file . "\n"; } }
$iterator = new RecursiveIteratorIterator(new RecursiveArrayIterator(array('file1.txt', array('file2.txt', 'file3.txt'), 'file4.txt'))); foreach($iterator as $file) { if($iterator->isFile()) { echo $file . "\n"; } }This code recursively iterates over an array that contains file names and nested arrays, and outputs the file names if they are files. The RecursiveIteratorIterator is part of the Standard PHP Library (SPL) package.