我正在寻找一种在目录中显示所有XML文件的方法。 Glob不按日期排序,至少我不认为,并且只能在同一目录中使用......我该怎么做?
答案 0 :(得分:3)
你可以很好地使用glob,但需要一些额外的代码来对它进行排序:
$files = glob("subdir/*");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
这将为您提供$filename => $timestamp
形式的关联数组。
答案 1 :(得分:1)
不太漂亮,但可能允许额外的功能:
<?php
// directory to scan
$dir = 'lib';
// put list of files into $files
$files = scandir($dir);
// remove self ('.') and parent ('..') from list
$files = array_diff($files, array('.', '..'));
foreach ($files as $file) {
// make a path
$path = $dir . '/' . $file;
// verify the file exists and we can read it
if (is_file($path) && file_exists($path) && is_readable($path)) {
$sorted[] = array(
'ctime'=>filectime($path),
'mtime'=>filemtime($path),
'atime'=>fileatime($path),
'filename'=>$path,
'filesize'=>filesize($path)
);
}
}
// sort by index (ctime)
asort($sorted);
// reindex and show our sorted array
print_r(array_values($sorted));
?>
<强>输出:强>
Array
(
[0] => Array
(
[ctime] => 1289415301
[mtime] => 1289415301
[atime] => 1299182410
[filename] => lib/example_lib3.php
[filesize] => 36104
)
[1] => Array
(
[ctime] => 1297202755
[mtime] => 1297202722
[atime] => 1297202721
[filename] => lib/example_lib1.php
[filesize] => 16721
)
[2] => Array
(
[ctime] => 1297365112
[mtime] => 1297365112
[atime] => 1297365109
[filename] => lib/example_lib2.php
[filesize] => 57778
)
)