此代码按字母顺序对图像进行排序。如何按日期排序?因此用户可以更方便地查看新图像。
以下是代码:
<link href="assets/css/gallery.css" rel="stylesheet">
<link href="assets/css/dope.css" rel="stylesheet">
<div class="row center">
<ul class="uk-tab" id="gamemodelistregion" uk-switcher="connect: + ul; animation: uk-animation-fade">
<li aria-expanded="true" class="uk-active">
<a>All skins</a>
<?php
# Skin directory relative to include/gallery.php (this file)
$skindir = "../skins/";
# Skin directory relative to index.html
$skindirhtml = "./skins/";
$images = scandir($skindir);
foreach($images as $curimg) {
if (strtolower(pathinfo($curimg, PATHINFO_EXTENSION)) == "png") {
?>
<li class="skin" onclick="$('#nick').val('{' + $(this).find('.title').text() + '} ');" data-dismiss="modal">
<div class="circular" style='background-image: url("./<?php echo $skindirhtml.$curimg ?>")'></div>
<h4 class="title"><?php echo pathinfo($curimg, PATHINFO_FILENAME); ?></h4>
</li>
<?php
}
}
?>
</li>
</ul>
</div>
答案 0 :(得分:1)
filemtime
函数返回文件上次修改日期的Unix时间戳。
// retrieve all images but don't waste time putting them in alphabetical order
$images = scandir($skindir, SCANDIR_SORT_NONE);
// remove all non-existent files and non-PNG files
$images = array_filter($images,
function($file) use ($skindir) {
$path = "$skindir/$file";
return file_exists($path) && strtolower(pathinfo($path, PATHINFO_EXTENSION)) == "png";
}
);
// sort image by modification time in descending order
usort($images, function($a,$b){return filemtime($a)-filemtime($b);});
// now you can iterate through images and print them
foreach($images as $curimg): ?>
<!-- Output image HTML here -->
<?php
endforeach;