Get last modified file in a directory

前端 未结 4 1976
时光说笑
时光说笑 2020-11-30 10:23

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

4条回答
  •  死守一世寂寞
    2020-11-30 10:55

    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);
    

提交回复
热议问题