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);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…