How do you get a directory listing sorted by creation date in PHP?
Question
I have to list directories by using scandir() function but result array should be sorted according to date and time of directory created.
Regards Deepak
Solution
you could try backticks
$listOfFiles = `ls -ltr`;
and then use explode to get the into an array format
OTHER TIPS
With scandir() you cannot.
Check this
Get results you need into an array and sort it... I'm not aware of a scandir option that can do this directly.
Problem can be solved by using filemtime($dirPath)
function.
code sample:
if (file_exists($dirPath)) {
echo "last modified: " . date ("F d Y H:i:s.", filemtime($dirPath));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow