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

LVL 4
kent3800Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nbandanCommented:
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

0
kent3800Author Commented:
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

0
kent3800Author Commented:
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

0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

kent3800Author Commented:
files on server listed: ls -l
server.png
0
nbandanCommented:
That is strange. Give this code a try.
$files = glob('images/original/*.*');
  array_multisort(
    array_map('filemtime', $files),
    SORT_NUMERIC,
    SORT_DESC,
    $files
  );

  print_r($files);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rok-KraljCommented:
Small and efficient

Source: http://www.jonasjohn.de/snippets/php/listdir-by-date.htm


function listdir_by_date($path){
    $dir = opendir($path);
    $list = array();
    while($file = readdir($dir)){
        if ($file != '.' and $file != '..'){
            // add the filename, to be sure not to
            // overwrite a array key
            $ctime = filectime($data_path . $file) . ',' . $file;
            $list[$ctime] = $file;
        }
    }
    closedir($dir);
    krsort($list);
    return $list;
}

echo implode('<br>', listdir_by_date('.'));

Open in new window

0
kent3800Author Commented:
both work. thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux Distributions

From novice to tech pro — start learning today.