How can one efficiently list files in a directory along with their respective modification dates using PHP?
To efficiently list files in a directory along with their respective modification dates using PHP, you can use the `scandir()` function to get an array of files in the directory, then loop through each file to get its modification date using `filemtime()`. Finally, you can display the file name along with its modification date.
$directory = "/path/to/directory";
$files = scandir($directory);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$modificationDate = date("Y-m-d H:i:s", filemtime($directory . '/' . $file));
echo "File: $file | Modification Date: $modificationDate <br>";
}
}
Keywords
Related Questions
- What is the best practice for adding a line to a file in PHP?
- How can developers ensure that a word censor function effectively filters out inappropriate language while maintaining the functionality of the messaging system in PHP?
- How can the use of code tags and proper formatting improve the readability and clarity of PHP code in forums?