2013-10-14 56 views
-1

我在YT上找到了一個php代碼來顯示目錄中的圖像。 一切都很完美,但我需要在最上面顯示最新的照片。 任何人都可以幫助我嗎?我如何排序我的圖像 - 最新最熱門

<?php 
    $dir = 'foto'; 
    $file_display = array('jpg', 'jpeg'); 

    if (file_exists($dir) == false) { 
     echo 'Gallery \'', $dir, '\' not found!'; 
    } else { 
     $dir_contents = scandir($dir); 

     foreach ($dir_contents as $file) { 
      $file_type = strtolower (end(explode('.', $file))); 

      if ($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) { 
       echo '<div id="', $file, '"><img src="', $dir, '/', $file, '" alt="', $file, '" /></div>'; 
      } 
     } 
    } 
?> 
+0

我認爲您需要查看PHP filemtime函數,以便取出文件的修改日期 - 我認爲單獨使用scandir不可能。然後看看這裏http://php.net/manual/en/array.sorting.php關於排序數組的信息,並將它們拼接在一起,你應該可以做到這一點。 –

+0

http://stackoverflow.com/questions/11923235/scandir-to-sort-by-date-modified可能的重複 – dave

回答

0

我想你會需要按日期排序圖像。有人在這裏寫了scandir()函數的另一個變體:https://stackoverflow.com/a/11923516/1644017

您可以在代碼中獲得該函數並將$dir_contents = scandir($dir);更改爲:$dir_contents = scan_dir($dir);

1

更少的代碼:

array_multisort((
    array_map(
     'filemtime', ($files = glob(
      "$dir/*.{jpg,jpeg}", GLOB_BRACE)))), SORT_DESC, $files); 

foreach所$文件和顯示。