What are some best practices for integrating the stat() function within a while loop in PHP to display file attributes like size and creation date?
When using the stat() function within a while loop in PHP to display file attributes like size and creation date, it is important to ensure that the file path is correctly specified for each iteration of the loop. This can be achieved by dynamically updating the file path within the loop. Additionally, it is advisable to check if the file exists before attempting to retrieve its attributes to avoid errors.
<?php
$files = glob('path/to/files/*');
foreach ($files as $file) {
if (is_file($file)) {
$fileInfo = stat($file);
echo "File: $file | Size: " . $fileInfo['size'] . " bytes | Created: " . date('Y-m-d H:i:s', $fileInfo['ctime']) . "<br>";
}
}
?>