Is there a way to select only the last file in a directory (with the extensions jpg|png|gif
?)
Or do I have to parse the entire directory and check using filemtime
?
Yes you have to read through them all. But since directory accesses are cached, you shouldn't really worry about it.
$files = array_merge(glob("img/*.png"), glob("img/*.jpg"));
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
$latest_file = key($files);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With