How can I modify the while() loop to output file names, sizes, and creation dates in PHP?
To output file names, sizes, and creation dates in PHP using a while() loop, you can use the opendir() function to open a directory, then loop through the files using readdir() to get each file's information. You can use functions like filesize() and filectime() to get the size and creation date of each file.
$dir = "path/to/directory";
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
if ($file != "." && $file != "..") {
$filePath = $dir . "/" . $file;
$size = filesize($filePath);
$creationDate = filectime($filePath);
echo "File: $file | Size: $size bytes | Creation Date: " . date("Y-m-d H:i:s", $creationDate) . "<br>";
}
}
closedir($dh);
}
}
Keywords
Related Questions
- What is the recommended method in PHP to modify a string like 'test,test1,test2,test3' to have each value surrounded by two single quotes?
- How can syntax errors, such as unexpected characters like '{', be effectively identified and resolved in PHP code?
- What are the potential consequences of creating multiple pages with the same content for SEO purposes?