0

I want to sort files in descending order with date to diplay, And using Grep Only

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"));
Rishi
  • 75
  • 10

1 Answers1

0

You can do something like this:

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");
kitensei
  • 2,510
  • 2
  • 42
  • 68
  • The given script works but, **Suppose C:\Workspace contains folder abc and it has file abc.txt,** It not shown in the result The out put is **C:\Workspace\abc** i wanted as **C:\Workspace\abc\abc.txt** In short it should display all the files for cotaining folder & Sub folders. Is it possible to do so? – Rishi Oct 17 '14 at 12:41
  • check [this post](http://stackoverflow.com/questions/14304935/php-listing-all-directories-and-sub-directories-recursively-in-drop-down-menu) for recursive solutions – kitensei Oct 17 '14 at 12:53
  • Thanks, Any expert guy who could update above code to show in **Recursive files using Grep Only** – Rishi Oct 18 '14 at 06:06