1

Possible Duplicate:
get last modified file in a dir?

I have a folder which contents more than 30000 subfolders in it. How can I get a list of subfolders with last modification date >= one hour ago? Is it possible to do that without getting a list of all files in an array and sorting it? I cannot use a readdir function because it returns files in the order in which they are stored by the filesystem and exhaustive search of the list of files will take a very long time.

Community
  • 1
  • 1
khomyakoshka
  • 1,259
  • 8
  • 18

3 Answers3

1

Use GNU Find - it is simpler and faster!

find [path] -type d -mmin +60
Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
1

The linux "find" command is pretty powerful.

$cmd = "find ".$path." type -d -mmin +60";
$out=`$cmd`;
$files=explode("\n",$out);
Clayton Bell
  • 424
  • 4
  • 4
-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++;
  }
}
?>
rOcKiNg RhO
  • 631
  • 1
  • 6
  • 16