如何按“最后修改日期”的顺序列出目录中的文件?(在Linux上使用PHP5)
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 />';
}
感谢这里的贡献。
readdir
将目录中的文件读取到数组中,并保存它们的filemtime
。根据这个值对数组进行排序,即可获得结果。DirectoryIterator
}}迭代目录中的文件SplFileInfo :: getMTime
}}获取其最后修改时间asort
}}或{{link4:arsort
}}对数组进行排序-具体取决于您想要的文件顺序。$files = array();
$dir = new DirectoryIterator(dirname(__FILE__));
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
$files[$fileinfo->getFilename()] = $fileinfo->getMtime();
}
}
arsort($files);
var_dump($files);
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
即列出保存我的脚本的目录中的文件列表,最近修改的文件位于列表开头。