[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2371
  • Last Modified:

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

0
kent3800
Asked:
kent3800
  • 4
  • 2
2 Solutions
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now