0

Is it possible to get the file names in a folder by the date of creation or modification?

Thanks.

usertest
  • 27,132
  • 30
  • 72
  • 94

3 Answers3

2
<?php
    $files = array();
    $it = new DirectoryIterator(".");
    $it->rewind();
    while ($it->valid()) { 
        $files[$it->getFilename()] = $it->getMTime(); 
        $it->next();
    }

    asort($files);
    $files = array_keys($files);
Artefacto
  • 96,375
  • 17
  • 202
  • 225
1

Get a list of the file names using normal methods (glob(), scandir(), whatever), and store the filenames in an array. Then loop through that array using filemtime() and store that value against your array. Finally, sort the array by your stored filemtime() value

Alternatively, look at DirectoryIterator and Sorting files by creation/modification date in PHP

Community
  • 1
  • 1
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

Yep, you need readdir() and filemtime().

rook
  • 66,304
  • 38
  • 162
  • 239