Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: ordered directory listing

Tags:

php

How to list files in a directory in "last modified date" order? (PHP5 on Linux)

like image 583
Dawid Ohia Avatar asked Dec 02 '22 06:12

Dawid Ohia


2 Answers

function newest($a, $b) 
{ 
    return filemtime($a) - filemtime($b); 
} 

$dir = glob('files/*'); // put all files in an array 
uasort($dir, "newest"); // sort the array by calling newest() 

foreach($dir as $file) 
{ 
    echo basename($file).'<br />'; 
} 

Credit goes here.

like image 143
ChristopheD Avatar answered Dec 19 '22 19:12

ChristopheD


read files in a directory by using readdir to an array along with their filemtime saved. Sort the array based on this value, and you get the results.

like image 43
Pentium10 Avatar answered Dec 19 '22 19:12

Pentium10