php如何在目录中获取最后修改的文件

时间:2012-12-12 05:07:08

标签: php directory

  

可能重复:
  get last modified file in a dir?

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

3 个答案:

答案 0 :(得分:1)

使用GNU查找 - 它更简单,更快捷!

find [path] -type d -mmin +60

答案 1 :(得分:1)

linux“find”命令非常强大。

$cmd = "find ".$path." type -d -mmin +60";
$out=`$cmd`;
$files=explode("\n",$out);

答案 2 :(得分:-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++;
  }
}
?>
相关问题