문제

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.

도움이 되었습니까?

해결책

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;
   }
?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top