Solved

ReadDir (php) sorting problem - sort by date required

Posted on 2004-10-10
2
2,587 Views
Last Modified: 2012-05-05
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);
?>
0
Comment
Question by:stephenreed
2 Comments
 
LVL 33

Accepted Solution

by:
sajuks earned 125 total points
ID: 12273713
//adjust according to your needs
<?
$files = array();
$files_directory = "c:/path";
$fdirectory = opendir("$files_directory");
    while (false!==($file=readdir($fdirectory))) {
        if (($file != ".") && ($file != ".." )) {
        $files[$file] = filemtime("$files_directory/$file");
        }
    }
closedir($fdirectory);
clearstatcache();
arsort($files);
echo "<table>";
while(list($key, $val)=each($files))
    echo "<tr><td>$key</td><td>".date("M j, h:ia", $val)."</td></tr>\n";
echo "</table>";
?>
0
 

Author Comment

by:stephenreed
ID: 12281941
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now