Php Grep按日期降序排序文件与日期

时间:2014-10-17 11:59:38

标签: php grep

我想按降序对文件进行排序,并显示日期,并使用仅限Grep

define("SLASH", stristr($_SERVER[SERVER_SOFTWARE], "win") ? "\\" : "/");
    function php_grep($path){

        $fp = opendir($path);
        while($f = readdir($fp)){
            if( preg_match("#^\.+$#", $f) ) continue; // ignore symbolic links
            $file_full_path = $path.SLASH.$f;
            if($file_full_path) {
                $ret .= "$file_full_path\n";
            }
        }
        return $ret;
    }
echo "<pre>";
print_r(php_grep("/home"));

1 个答案:

答案 0 :(得分:0)

您可以这样做:

function php_grep($path){
    $ret = array();
    $dates = array();
    $fp = opendir($path);
    while($f = readdir($fp)){
        if( preg_match("#^\.+$#", $f) ) continue; // ignore symbolic links
        $file_full_path = $path.DIRECTORY_SEPARATOR.$f;
        if($file_full_path) {
            $ret[] = $file_full_path;
        }
    }

    array_multisort(array_map('filemtime', $ret), SORT_NUMERIC, SORT_DESC, $ret);
    return $ret;
}
echo "<pre>";
php_grep("C:\Workspace");