Php sort file from dir in desending order (Latest at top) -
**files date descending order, how it? ** need show files date latest @ top
function list_dir($dn){ if($dn[strlen($dn)-1] != '\\') $dn.='\\'; static $ra = array(); $handle = opendir($dn); while($fn = readdir($handle)){ if($fn == '.' || $fn == '..') continue; if(is_dir($dn.$fn)) list_dir($dn.$fn.'\\'); else $ra[] = $dn.$fn; } closedir($handle); return $ra; } $filelist = list_dir('d:\xampp\htdocs'); for($i=0;$i<count($filelist);$i++){ $test = array(); $year = $test[2]; $day = $test[1]; $month = $test[0]; $test = explode("/",date("m/d/y",filemtime($filelist[$i]))); echo "<span style='color:red;'><b style='color:green;'>".$day.'-'.month.'-'.$year. '</b> ' . $filelist[$i]."</span><br>"; } clearstatcache();
you can use glob
, arsort
sort high low.
$files = glob("*"); //fetch files. $files = array_combine($files, array_map("filemtime", $files)); //grab filetime each file arsort($files); //sort high low echo "<pre>"; echo print_r($files, true); echo "</pre>";
output (after formatting in table)
<?php $files = glob("*"); //fetch files. $files = array_combine($files, array_map("filemtime", $files)); arsort($files); echo "<table> <tr> <th>file</th> <th>last modified</th>"; foreach( $files $file => $date ) { echo '<tr><td>'. $file .'</td><td>'. date("y m d g:i:s a", $date).'</td></tr>'; } echo "</table>";
Comments
Post a Comment