PHP:有序目录列表
如何按“上次修改日期”顺序列出目录中的文件?(Linux上的PHP5)在谷歌上尝试同样的查询,你会更快地得到答案。干杯PHP:有序目录列表,php,Php,如何按“上次修改日期”顺序列出目录中的文件?(Linux上的PHP5)在谷歌上尝试同样的查询,你会更快地得到答案。干杯 另一个来自谷歌的解决方案是: 迭代目录中的文件——例如使用 对于每个文件,使用 将所有数据放入一个数组中,并使用: 这些文件的名称为键 修改时间作为值 并对数组进行排序,根据文件的顺序,使用或 例如,这部分代码: $files = array(); $dir = new DirectoryIterator(dirname(__FILE__)); foreach ($d
另一个来自谷歌的解决方案是:
- 迭代目录中的文件——例如使用
- 对于每个文件,使用
- 将所有数据放入一个数组中,并使用:
- 这些文件的名称为键
- 修改时间作为值
- 并对数组进行排序,根据文件的顺序,使用或
例如,这部分代码:
$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
i、 e.保存脚本的目录中的文件列表,最新修改的文件位于列表开头。使用
readdir
将目录中的文件及其filemtime
保存的文件一起读取到数组中。根据此值对数组进行排序,然后得到结果。函数最新($a,$b)
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 />';
}
{
返回filemtime($a)-filemtime($b);
}
$dir=glob('files/*');//将所有文件放入一个数组中
uasort($dir,“最新”);//通过调用最新()对数组进行排序
foreach($dir作为$file)
{
echo basename($file)。“
”;
}
信贷。解决方案不错,但无法扩展。