$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/directory')); foreach ($iterator as $file) { if ($file->isFile()) { echo $file->getPathname() . "\n"; } }
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('/path/to/directory')); $count = 0; foreach ($iterator as $file) { if ($file->isFile()) { $count++; } } echo "There are $count files in the directory tree.\n";In this example, we count the number of files in a directory tree by iterating over all files and incrementing a counter for each file encountered. We use the `getPathname()` method to check if each item in the iterator is a file. These examples demonstrate how the `RecursiveIteratorIterator::getPathname()` method can be used to work with complex file structures in PHP. This method is part of the Standard PHP Library (SPL).