Solved

ReadDir (php) sorting problem - sort by date required

Posted on 2004-10-10
2
2,586 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

14 Experts available now in Live!

Get 1:1 Help Now