开发者

Get most recent files recursively with PHP

I am looking for code which lists the five most recent files in a directory recursively.

This is non-recursive code, and would be perfect for me if it was recursive:

<?php

$show = 开发者_JAVA百科0; // Leave as 0 for all
$dir = 'sat/'; // Leave as blank for current

if($dir) chdir($dir);
$files = glob( '*.{html,php,php4,txt}', GLOB_BRACE );
usort( $files, 'filemtime_compare' );

function filemtime_compare( $a, $b )
{
    return filemtime( $b ) - filemtime( $a );
}
$i = 0;
foreach ( $files as $file )
{
    ++$i;
    if ( $i == $show ) break;
    echo $file . ' - ' . date( 'D, d M y H:i:s', filemtime($file) ) . '<br />' . "\n";  /* This is the output line */
}
?>

It is possible to modify it to scan directories recursively?


This was my first version (tested, working):

function latest($searchDir, array $files = array()) {
    $search = opendir($searchDir);

    $dirs = array();
    while($item = readdir($search)) {
        if ($item == '.' || $item == '..') { continue; }
        if (is_dir($searchDir.'/'.$item)) {
            $dirs[] = $searchDir.'/'.$item;
        }
        if (is_file($searchDir.'/'.$item)) {
            $ftime = filemtime($searchDir.'/'.$item);
            $files[$ftime] = $searchDir.'/'.$item;
        }
    }
    closedir($search);
    if (count($dirs) > 0) {
        foreach ($dirs as $dir) {
            $files += latest($dir,$files);
        }
    }
    krsort($files);
    $files = array_slice($files, 0, 5, true);
    return $files;
}

But I like byte's usage of glob(), so here is a slightly modified version of his to return the same format:

function top5modsEx($dir) {
    $mods = array();
    foreach (glob($dir . '/*') as $f) {
        $mods[filemtime($f)] = $f;
    }
    krsort($mods);
    return array_slice($mods, 0, 5, true);
}

This returns the time (UNIX Timestamp format) that the file was modified as the key of the element in the array.


This is pretty quick and dirty, and untested, but might get you started:

function top5mods($dir)
{
  $mods = array();
  foreach (glob($dir . '/*') as $f) {
    $mods[] = filemtime($f);
  }
  sort($mods);
  $mods = array_reverse($mods);
  return array_slice($mods, 0, 5);
}


Check out this solution in the PHP manual.


Edit : Sorry I didn't see the part "recursively".

To get RECENTS files first (html for example), please sort like this with anonymous function :

$myarray = glob("*.*.html");
usort($myarray, function($a,$b){
  return filemtime($b) - filemtime($a);
});

And to get it recursively you can :

function glob_recursive($pattern, $flags = 0) {
    $files = glob($pattern, $flags);
    foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
        $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
    }
    return $files;
}

So, replace then the glob function by :

$myarray = glob_recursive("*.*.html");
usort($myarray, function($a,$b){
  return filemtime($b) - filemtime($a);
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜