Computer Science Canada

List Files In Directory by Last Modified

Author:  Numbah51 [ Fri Feb 09, 2007 11:20 pm ]
Post subject:  List Files In Directory by Last Modified

I wanna list the files in a directory with the last modified (added) at the top, then the next, etc. Right now my code is:
(Right now it lists alphabetically)

code:

<?php
if ($handle = opendir('Content\Videos')) {

    // List all the files
    while (false !== ($file = readdir($handle))) {
        if ($file <> "."){
                if ($file <> ".." ){
                        if ($file <> "Thumbs.db" ){
                                echo "$file<BR>";
                        }
                }
        }
    }

    closedir($handle);
}

?> 


Any Help Would Be Greatly Appreciated.

EDIT: (I'll leave original message)
i found this code:
code:

<?PHP

$directory="Content/Videos/";
$sortOrder="newestFirst";

   $results = array();
   $handler = opendir($directory);
   
while ($file = readdir($handler)) { 
       if ($file != '.' && $file != '..' && $file != "robots.txt" && $file != ".htaccess"){
           $currentModified = filectime($directory."/".$file);
           $file_names[] = $file;
           $file_dates[] = $currentModified;
       }   
   }
       closedir($handler);

   //Sort the date array by preferred order
   if ($sortOrder == "newestFirst"){
       arsort($file_dates);
   }else{
       asort($file_dates);
   }
   
   //Match file_names array to file_dates array
   $file_names_Array = array_keys($file_dates);
   foreach ($file_names_Array as $idx => $name) $name=$file_names[$name];
   $file_dates = array_merge($file_dates);
   
   $i = 0;

   //Loop through dates array and then echo the list
   foreach ($file_dates as $$file_dates){
       $date = $file_dates;
       $j = $file_names_Array[$i];
       $file = $file_names[$j];
       $i++;
           
       echo  "File name: $file - Date Added: $date. <br/>"";       
   }

?>


It doesn't display anything. I can tell the error is in the foreach loop but i cannot seem to fix it


EDIT:
I think I got it. There was an Extra "

Author:  wtd [ Sat Feb 10, 2007 9:49 am ]
Post subject:  RE:List Files In Directory by Last Modified

Investigate array_multisort.

Author:  Waffa [ Tue Dec 01, 2009 3:20 am ]
Post subject:  Re: List Files In Directory by Last Modified

I will get error whit the last skript:

Parse error: syntax error, unexpected '"', expecting ',' or ';' in /home/v3451/public_html/OSc/muudetud.php on line 39

- do i need to change directory also in your last script or it will list this root directory & subdirectory files where the php file is uploaded?




I use my self this script but it does not arrange files by date:
code:

<?php
//Starts Here
//Put here the directory you want to search for. Put / if you want to search your entire domain
$dir='[color=#0008FF][/color]';

//Put the date you want to compare with in the format of:  YYYY-mm-dd hh:mm:ss
$comparedatestr="2006-08-12 00:00:00";
$comparedate=strtotime($comparedatestr);

//I run the function here to start the search.
directory_tree($dir,$comparedate);

//This is the function which is doing the search...
function directory_tree($address,$comparedate){

 @$dir = opendir($address);

  if(!$dir){ return 0; }
        while($entry = readdir($dir)){
                if(is_dir("$address/$entry") && ($entry != ".." && $entry != ".")){                             
                        directory_tree("$address/$entry",$comparedate);
                }
                 else   {

                  if($entry != ".." && $entry != ".") {
                 
                    $fulldir=$address.'/'.$entry;
                    $last_modified = filemtime($fulldir);
                    $last_modified_str= date("Y-m-d h:i:s", $last_modified);

                       if($comparedate < $last_modified)  {
                          echo $fulldir.'=>'.$last_modified_str;
                          echo "<BR>";
                       }

                 }

            }

      }

}
?>

Author:  jeffgreco13 [ Tue Dec 01, 2009 12:32 pm ]
Post subject:  Re: List Files In Directory by Last Modified

look into...
http://php.net/manual/en/function.filemtime.php
.. this returns a UNIX timestamp to be used with the date() function in PHP.

http://www.php.net/manual/en/function.date.php

i recommend using:
code:
date('U');


it gives u the amount of seconds of the given timestamp since the UNIX epoch. Therefore highest number = latest modified.

You could try and get something going with that.


: