Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get filemtime for most recently updated file in folder

Tags:

php

filemtime

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?

like image 859
ryanve Avatar asked Jan 20 '23 04:01

ryanve


1 Answers

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();
        }
    }
}
like image 114
David Snabel-Caunt Avatar answered Jan 28 '23 09:01

David Snabel-Caunt