Get filemtime for most recently updated file in folder

人盡茶涼 提交于 2019-11-29 15:19:39

问题


I have a folder with 4 files in it and I'd like to pull the last modified time of the most recent one (which may not always be the same one). Is there a good way to do that?


回答1:


Use a DirectoryIterator to find the files and then simply compare their modified times. This oughta do it:

$iterator = new DirectoryIterator('path/to/dir');

$mtime = -1;
$file;
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        if ($fileinfo->getMTime() > $mtime) {
            $file = $fileinfo->getFilename();
            $mtime = $fileinfo->getMTime();
        }
    }
}



回答2:


There is no need to iterate through the directory - filemtime will work for most servers, (depending on your configuration):

$LastMod = filemtime("/path/to/dir/.");

The last dot is needed to see the directory as a file and to actually get a last modification date of it.



来源:https://stackoverflow.com/questions/6767346/get-filemtime-for-most-recently-updated-file-in-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!