Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get last modified file in a directory

Tags:

php

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?

like image 925
dynamic Avatar asked Mar 27 '11 09:03

dynamic


1 Answers

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);
like image 176
mario Avatar answered Sep 27 '22 21:09

mario