Dynamically I am creating sub directory in a folder and I need to get the last created directory in a sting format, I have tried many ways but failed to get it. What i have tried is,
$path = "/path/to/my/dir";
$latest_ctime = 0;
$latest_filename = '';
$d = dir($path);
while (false !== ($entry = $d->read())) {
$filepath = "{$path}/{$entry}";
if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
$latest_ctime = filectime($filepath);
$latest_filename = $entry;
}
}
But it works for only file.
Thanks in advance.
2
Answers
Started as comments – but there were so many!
change
is_file
tois_dir
$path in the above is not a URL, its a path. is_dir/is_file/filectime won’t work for URLs
Also you’ve flagged this as cpanel which would be a reason for closing this as off-topic, but its not relevant to a coding problem. OTOH it does hint that this is not a MSWindows platform. On Unix and Linux, filectime() actually returns the filemtime. So the information it shows will not accurately reflect the creation time.
Try something like this:
Also,
you can change the condition of the blacklist for:
to exclude hidden directories (in unix-like sistems).