Link to home
Start Free TrialLog in
Avatar of kent3800
kent3800

asked on

php readdir - how can you list the files by date?

The below script returns the list of all the files in the specified directory by the default in reverse alphabetical order.

How can I list the contents of the directory by date created?

Is there a directory listing default in debian Ubuntu linux?

Or how can I get the file information from the server i.e. the date to build an array to later be sorted by the stored date? How would I build that php array?

Thanks
<?php
$directory = "images/original";
$handler = opendir($directory);
while (false !== ($file = readdir($handler))) {
    if ($file != "." && $file != "..") {
  	echo '<img class="libraryImage" src="' . $directory . '/' . $file . '" alt="' . $file . '" />';
    }
}
closedir($handler);
	  ?>
	</div>

Open in new window

Avatar of nbandan
nbandan

Here is some code that will read the directory and sort the file names for you by date.
<?php 
  function LoadFiles($dir) {
    $Files = array();
    $It =  opendir($dir);
    if (! $It)
      die('Cannot list files for ' . $dir);
 
      while ($Filename = readdir($It)) {
        if ($Filename == '.' || $Filename == '..')
        continue;
        
        $LastModified = filemtime($dir . $Filename);
        $Files[] = array($dir .$Filename, $LastModified);
      }

    return $Files;
  }

  function DateCmp($a, $b) {
    return ($a[1] < $b[1]) ? -1 : 0;
  }

  function SortByDate(&$Files) {
    usort($Files, 'DateCmp');
  }

  $Files = LoadFiles('images/original');
  SortByDate($Files);
?>

Open in new window

Avatar of kent3800

ASKER

Thanks this generally works. It isn't sorting some of the files though. Here are the results from the SortByDate, server directory image, and my altered code of your functions. Any idea what I'm doing wrong?
function LoadFiles($dir) {
    $Files = array();
    $handler =  opendir($dir);
    if (!$handler)
      die('Cannot list files for ' . $dir);
 
      while ($Filename = readdir($handler)) {
        if ($Filename == '.' || $Filename == '..')
        continue;
        
        $LastModified = filemtime($dir . $Filename);
        $Files[] = array($dir . $Filename, $LastModified);
      }

    return $Files;
  }

  function DateCmp($a, $b) {
    return ($a[1] < $b[1]) ? -1 : 0;
  }

  function SortByDate(&$Files) {
    usort($Files, 'DateCmp');
  }

  $Files = LoadFiles('images/original/');
  SortByDate($Files);
  
  foreach($Files as $source)
  	echo '<img class="libraryImage" src="/' . $source[0] . '" alt="' . date("F d Y H:i:s", $source[1]) . '" />';
?>

Open in new window

The result:
<img class="libraryImage" src="/images/original/IceClimb2.jpg" alt="April 06 2010 00:27:21" />
<img class="libraryImage" src="/images/original/Katz_R_02.jpg" alt="April 06 2010 00:27:27" />
<img class="libraryImage" src="/images/original/Hut_Ski_tommy2.jpg" alt="April 06 2010 00:27:14" />
<img class="libraryImage" src="/images/original/Hochtl_kevin1.jpg" alt="April 06 2010 00:27:10" />
<img class="libraryImage" src="/images/original/Weiland_Dan15.jpg" alt="April 06 2010 00:51:47" />
<img class="libraryImage" src="/images/original/hikinglakecharles.jpg" alt="April 06 2010 00:27:02" />
<img class="libraryImage" src="/images/original/Masinter_M_07.jpg" alt="April 06 2010 00:27:32" />
<img class="libraryImage" src="/images/original/PhiliponM_01.jpg" alt="April 06 2010 00:27:43" />
<img class="libraryImage" src="/images/original/StoneCrk1.jpg" alt="April 06 2010 00:28:06" />
<img class="libraryImage" src="/images/original/Snowshoe2.jpg" alt="April 06 2010 00:28:01" />
<img class="libraryImage" src="/images/original/RubyMtn_19.jpg" alt="April 06 2010 00:27:52" />
<img class="libraryImage" src="/images/original/Ravinos2.jpg" alt="April 06 2010 00:27:48" />
<img class="libraryImage" src="/images/original/HikingGore.jpg" alt="April 06 2010 00:26:53" />
<img class="libraryImage" src="/images/original/hikinggolddust.jpg" alt="April 06 2010 00:26:45" />
<img class="libraryImage" src="/images/original/catskiingkeystone1.jpg" alt="April 06 2010 00:26:23" />
<img class="libraryImage" src="/images/original/BCGondola1.jpg" alt="April 06 2010 00:26:20" />
<img class="libraryImage" src="/images/original/xcountry_goldberg_nate.jpg" alt="April 06 2010 00:22:00" />
<img class="libraryImage" src="/images/original/Allegria_34.jpg" alt="April 06 2010 00:20:57" />
<img class="libraryImage" src="/images/original/CordilleraLodge3.jpg" alt="April 06 2010 00:26:26" />
<img class="libraryImage" src="/images/original/Dog&Board.jpg" alt="April 06 2010 00:26:28" />
<img class="libraryImage" src="/images/original/GoreHorses.jpg" alt="April 06 2010 00:26:42" />
<img class="libraryImage" src="/images/original/frank shine snowmass.jpg" alt="April 06 2010 00:26:38" />
<img class="libraryImage" src="/images/original/FlyFishingGoreWinter.jpg" alt="April 06 2010 00:26:38" />
<img class="libraryImage" src="/images/original/elkbull2.jpg" alt="April 06 2010 00:26:33" />
<img class="libraryImage" src="/images/original/ballooncamelot2.jpg" alt="April 06 2010 00:20:44" />

Open in new window

files on server listed: ls -l
server.png
ASKER CERTIFIED SOLUTION
Avatar of nbandan
nbandan

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
SOLUTION
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
both work. thanks