11

Possible Duplicate:
File creation time

In PHP, how to retrieve the files contained into a folder sorted by creation date (or any other sorting mechanism)?

According to the doc, the readdir() function:

The filenames are returned in the order in which they are stored by the filesystem.

Community
  • 1
  • 1
Roberto Aloi
  • 30,570
  • 21
  • 75
  • 112

6 Answers6

8

save their information to an array, sort the array and then loop the array

if($h = opendir($dir)) {
  $files = array();
  while(($file = readdir($h) !== FALSE)
    $files[] = stat($file);

  // do the sort
  usort($files, 'your_sorting_function');

  // do something with the files
  foreach($files as $file) {
    echo htmlspecialchars($file);
  }
}
knittl
  • 246,190
  • 53
  • 318
  • 364
6

That's my solution:

$fileList = array();
$files = glob('home/dir/*.txt');
foreach ($files as $file) {
    $fileList[filemtime($file)] = $file;
}
ksort($fileList);
$fileList = array_reverse($fileList, TRUE);
print_r($fileList);

output is like this:

array(
    (int) 1340625301 => '/home/dir/file15462.txt',
    (int) 1340516112 => '/home/dir/file165567.txt',
    (int) 1340401114 => '/home/dir/file16767.txt'
)

Then with "foreach" loop take the file you need.

conrad10781
  • 2,223
  • 19
  • 17
trante
  • 33,518
  • 47
  • 192
  • 272
  • Lets hope none of your files get modified within the same second of unix time. – salathe Jun 25 '12 at 17:42
  • I replaced the foreach body line with: $fileList[filemtime($file).$file] = $file; It solves the issue salathe mentions. I have the impression that this key conversion to string also makes ksort much faster for my 300k files. – Albert Hendriks Jan 14 '14 at 14:08
  • Will not work when there are two files with the SAME creation date :-/ – powtac Sep 03 '15 at 12:04
  • There is a capital 'L' missing in the last line. Should be: print_r($fileList); Apologies for posting it here but it says: "The suggested edit cue is full" – HelloWorld Nov 24 '20 at 10:15
4

You can store the files in an array, where the key is the filename and the value is the value to sort by (i.e. creation date) and use asort() on that array.

$files = array(
    'file1.txt' => 1267012304,
    'file3.txt' => 1267011892,
    'file2.txt' => 1266971321,
);
asort($files);
var_dump(array_keys($files));
# Output:
array(3) {
  [0]=>
  string(9) "file2.txt"
  [1]=>
  string(9) "file3.txt"
  [2]=>
  string(9) "file1.txt"
}
soulmerge
  • 73,842
  • 19
  • 118
  • 155
2

Heh, not even the great DirectoryIterator can do that out of the box. Sigh.

There seems to be a pretty powerful script to do all that referenced here: preg_find. I've never worked with it but it looks good.

sorted in by filesize, in descending order?
$files = preg_find('/./', $dir,
  PREG_FIND_RECURSIVE| PREG_FIND_RETURNASSOC |
  PREG_FIND_SORTFILESIZE|PREG_FIND_SORTDESC);

$files=array_keys($files);
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
0

You could use scandir() to read the contents of the directory into an array, then use fileatime() or filectime() to see each file's access or creation time, respectively. Sorting the array from here shouldn't be too hard.

jackbot
  • 2,931
  • 3
  • 27
  • 35
-2

use shell_exec to execute command on os-level? on a linux/unix box this might work;

$output=shell_exec('ls -t1');
futtta
  • 5,917
  • 2
  • 21
  • 33