Link to home
Start Free TrialLog in
Avatar of stephenreed
stephenreed

asked on

ReadDir (php) sorting problem - sort by date required

How would I sort the following code so I get it sorted by date. At present it lists files on my web page in Alphabetical order, I want it in date order.

<?php
//Get a cursor toward the folder of interest
$rep = "newsletters/";
$dir = opendir($rep);

// read the folder by selecting the folders (is_dir) or the folders (is_file)
   function dd($date) {
   //return date("m/d/Y H:i:s",$date);
   return date("d F Y",$date);
}
?><TABLE width="100%">
<!--<TR>
        <TD class="textbold" width="5%">&nbsp;</TD>
        <TD class="textbold" width="50%">Name</TD>
        <TD class="textbold" width="20%">Size</TD>
        <TD class="textbold" width="25%">Published</TD>
</TR> -->
<?
while ($f = readdir($dir)) {
   if(is_file($rep.$f)) {

   ?>
           <TR>
           <TD class="text" width="5%"><a href="<? echo "newsletters/".$f?>"><? echo "<img src=\"../media/pdf.gif\" border=0></a>"; ?></TD>
           <TD class="text" width="40%"><a href="<? echo "newsletters/".$f?>"><? echo $f;?></a></TD>
           <TD class="text" width="30%"><? echo filesize($rep.$f)." bytes";?></TD>
           <TD class="text" width="25%"><? echo dd(fileatime($rep.$f));?></TD>
           </TR>
  <?
  }
}
?></TABLE><?

//finally, close the folder
closedir($dir);
?>
ASKER CERTIFIED SOLUTION
Avatar of sajuks
sajuks

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stephenreed
stephenreed

ASKER

Used the above code but did not use arsort($files);
 but used the asort($files); function in its place and sorted by chronological order (date order). The explanation (code) was quite minimal and would require some programing ability so I would not give this answer full marks as it needed some instructions to modify for different sort orders.
Stephen