Question

Hello there good people of stackoverflow.

I have this code that basically get any photo i place in a folder called "pictures" and displays them on my page. Which is all good, and it works, with lightbox as well.

My main question is, can there be an easy way to display the photos in some form of order? i.e newest photos first?

<?php $handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){if($file !== '.' && $file !== '..'){
echo '<a href="pictures/'.$file.'" rel="lightbox"><img src="pictures/'.$file.'" border="0" /></a>';}}?>

I know the code is very vulgar and ancient, but its only for a sample page so it doesn't need to be massive.

Était-ce utile?

La solution

You can order them using php's filemtime(): http://php.net/manual/en/function.filemtime.php

<?php 
   $path = dirname(realpath(__FILE__)).'/pictures/';
   $handle = opendir($path);
   $arrFiles = array();

   while($file = readdir($handle))
   {
      if($file !== '.' && $file !== '..')
      {
          $arrFiles[filemtime($path.$file)] = '<a href="pictures/'.$file.'" rel="lightbox"><img src="pictures/'.$file.'" border="0" /></a>';
      }
   }
   arsort($arrFiles);
   foreach ($arrFiles as $file)
   {
       echo $file;
   }
?>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top