开发者

sorting scandir by date Descending order

i am using this code for listing a directory

foreach(scand开发者_运维百科ir('back/1') as $folder){
    if (in_array($folder, array('.', '..'))) continue;
    echo basename($folder); // get folder's name
}

but i want to sort the directories input by modifecation time.

how to do it.

Regards


$files = glob('back/1/*',GLOB_ONLYDIR);
foreach ($files as $f){
  $tmp[basename($f)] = filemtime($f);
}
asort($tmp);
$files = array_keys($tmp);


I am adapting Your Common Sense's answer to sort by descending last modified time stamp. The sort function is from joseph dot morphy's comment on the PHP glob spec page.

if (!function_exists('sort_by_mtime'))   {
    function sort_by_mtime($file1,$file2) {
        $time1 = filemtime($file1);
        $time2 = filemtime($file2);
        if ($time1 == $time2) {
            return 0;
        }
        return ($time1 < $time2) ? 1 : -1;
    }
}

$files = glob('back/1/*',GLOB_ONLYDIR);
foreach ($files as $f){
  $tmp[basename($f)] = filemtime($f);
}
usort($tmp,"sort_by_mtime");
$files = array_keys($tmp);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜