3
我正在尋找一種方法來顯示我的所有XML文件在一個目錄中。 Glob不按日期排序,至少我不這麼認爲,只能在同一個目錄中使用......我該怎麼做?如何從目錄獲取文件並使用PHP按日期/時間顯示它們?
我正在尋找一種方法來顯示我的所有XML文件在一個目錄中。 Glob不按日期排序,至少我不這麼認爲,只能在同一個目錄中使用......我該怎麼做?如何從目錄獲取文件並使用PHP按日期/時間顯示它們?
可以很好地使用水珠,但需要一些額外的代碼在事後對其進行排序:
$files = glob("subdir/*");
$files = array_combine($files, array_map("filemtime", $files));
arsort($files);
這會給你的形式的關聯數組。
不太漂亮,但可允許額外的功能:
<?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
)
)
水珠可以使用'safe_mode'一些系統被禁用。 ([信息](http://seclists.org/fulldisclosure/2005/Sep/1)) – drudge 2011-03-18 00:03:57