我在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>';
}
}
}
?>
我認爲您需要查看PHP filemtime函數,以便取出文件的修改日期 - 我認爲單獨使用scandir不可能。然後看看這裏http://php.net/manual/en/array.sorting.php關於排序數組的信息,並將它們拼接在一起,你應該可以做到這一點。 –
http://stackoverflow.com/questions/11923235/scandir-to-sort-by-date-modified可能的重複 – dave