Get last modified file in a directory

前端 未结 4 1975
时光说笑
时光说笑 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:51

    function listdirfile_by_date($path)
    {
        $dir = opendir($path);
        $list = array();
        while($file = readdir($dir))
        {
            if($file != '..' && $file != '.')
            {
                $mtime = filemtime($path . $file) . ',' . $file;
                $list[$mtime] = $file;
            }
        }
        closedir($dir);
        krsort($list);
    
        foreach($list as $key => $value)
        {
            return $list[$key];
        }
        return '';
    }
    

提交回复
热议问题