1

i have a question, can php get newest file in directory without scan all file? because i have a lot of folder with a hundred files, and it take so long to get newest file here's my code

$dir = new DirectoryIterator($d);
foreach ($dir as $f) {
    if ($f->isFile()) {
        $files[$f->getMTime()] = $d.$f->getFilename();
    }
}

and i have another alternative

if ($handle = opendir($d)) {
    while (false !== ($f = readdir($handle))) {
        $f = $d.$f;
        if(is_file($f)) {
            $files[filemtime($f)] = $f;
        }
    }
    closedir($handle);
}
zajonc
  • 1,935
  • 5
  • 20
  • 25
  • 3
    See http://stackoverflow.com/questions/5448374/get-last-modified-file-in-a-dir – fullybaked Apr 30 '13 at 08:40
  • but it seems more heavy to read dir with glob, isn't it? – Yessie Setiawan Apr 30 '13 at 08:47
  • If you read the comments to the accepted answer, they discuss that and why it is still better than readdir and looping – fullybaked Apr 30 '13 at 08:48
  • Who's creating these files? The ideal solution would be to have a symbolic link that always points to the newest version. Then you wouldn't need to scan the directory at all. – cleong Apr 30 '13 at 09:11
  • it's automatic to create these files every 15 seconds. Symbolic link like what? – Yessie Setiawan Apr 30 '13 at 09:13
  • so, for now, i think is impossible to get newest file wo scan directory, but i get another function, which faster to get all files. http://jdhsu.blogspot.com/2010/01/9-ways-to-iterate-over-directory-in-php.html – Yessie Setiawan May 01 '13 at 04:06

0 Answers0