The solution would be:
- Iterating over files in a directory - using
DirectoryIterator , for example - For each file, get its latest modification time using
SplFileInfo::getMTime - Put it all in an array using:
- file names in the form of keys
- Modification time as values
- And sort the array using
asort or arsort - depending on the order in which you want to store the files.
For example, this piece of code:
$files = array(); $dir = new DirectoryIterator(dirname(__FILE__)); foreach ($dir as $fileinfo) { if (!$fileinfo->isDot()) { $files[$fileinfo->getFilename()] = $fileinfo->getMtime(); } } arsort($files); var_dump($files);
Gives me:
array 'temp.php' => int 1268342782 'temp-2.php' => int 1268173222 'test-phpdoc' => int 1268113042 'notes.txt' => int 1267772039 'articles' => int 1267379193 'test.sh' => int 1266951264 'zend-server' => int 1266170857 'test-phing-1' => int 1264333265 'gmaps' => int 1264333265 'so.php' => int 1264333262 'prepend.php' => int 1264333262 'test-curl.php' => int 1264333260 '.htaccess' => int 1264333259
i.e. a list of files in the directory where my script is saved, with the last one changed at the top of the list.
Pascal martin
source share