Solved

ReadDir (php) sorting problem - sort by date required

Posted on 2004-10-10
2
2,591 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

739 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