What are some best practices for displaying additional information like modification date or file size when using RecursiveIteratorIterator in PHP?
When using RecursiveIteratorIterator in PHP to iterate over files and directories, you may want to display additional information like modification date or file size. One way to achieve this is by accessing the file information using the SplFileInfo class within the loop. This allows you to easily retrieve and display the desired information for each file or directory.
$directory = new RecursiveDirectoryIterator('path/to/directory');
$iterator = new RecursiveIteratorIterator($directory);
foreach ($iterator as $file) {
$info = new SplFileInfo($file);
echo 'File: ' . $info->getFilename() . ' | Size: ' . $info->getSize() . ' | Modified: ' . date('Y-m-d H:i:s', $info->getMTime()) . PHP_EOL;
}
Related Questions
- What are the advantages of using PDO over mysqli for database interactions in PHP?
- How can PHP developers ensure that uploaded files are properly handled and not lost during the process?
- What steps can be taken to prevent third-party modules from disappearing within the module management system in PHP?