1

可能重复:
在目录中获取最后修改的文件?

我有一个文件夹,其中包含超过 30000 个子文件夹。如何获取上次修改日期 >= 一小时前的子文件夹列表?是否可以在不获取数组中所有文件的列表并对其进行排序的情况下做到这一点?我不能使用readdir函数,因为它按照文件系统存储文件的顺序返回文件,并且对文件列表的详尽搜索将花费很长时间。

4

3 回答 3

1

使用 GNU Find - 它更简单、更快捷!

find [path] -type d -mmin +60
于 2012-12-12T05:20:26.910 回答
1

linux“查找”命令非常强大。

$cmd = "find ".$path." type -d -mmin +60";
$out=`$cmd`;
$files=explode("\n",$out);
于 2012-12-12T05:23:23.527 回答
-1
Give this a try:

<?php
$path = 'path/to/dir';

if (is_dir($path)) {
  $contents = scandir($path);

  foreach ($contents as $file) {
    $full_path = $path . DIRECTORY_SEPARATOR . $file;

    if ($file != '.' && $file != '..') {
      if (is_dir($full_path)) {
        $dirs[filemtime($full_path)] = $file;
      }
    }
  }

  // Sort in reverse order to put newest modification at the top
  krsort($dirs);

  $iteration = 0;

  foreach ($dirs as $mtime => $name) {
    if ($iteration != 5) {
      echo $name . '<br />';
    }

    $iteration++;
  }
}
?>
于 2012-12-12T05:28:10.037 回答