How can one display the file size and creation date for each file when using opendir() in PHP?
When using opendir() in PHP to read files from a directory, you can use the filemtime() function to get the creation date of each file. To display the file size, you can use the filesize() function. By combining these functions with the readdir() function to iterate through the files in the directory, you can display the file size and creation date for each file.
$dir = 'path/to/directory';
$dh = opendir($dir);
while (($file = readdir($dh)) !== false) {
if ($file != '.' && $file != '..') {
$filePath = $dir . '/' . $file;
$fileSize = filesize($filePath);
$fileCreationDate = date("Y-m-d H:i:s", filemtime($filePath));
echo "File: $file | Size: $fileSize bytes | Creation Date: $fileCreationDate <br>";
}
}
closedir($dh);
Keywords
Related Questions
- What are the best practices for finding and using necessary DLL files in PHP projects?
- How can PHP developers efficiently manage user sessions to ensure accurate online/offline status in a database without relying on manual intervention?
- In PHP, what are the benefits of using interfaces for defining functionalities like text formatting and data retrieval from databases?