Php 如何按修改日期对图像进行排序?
此代码按字母顺序对图像进行排序。如何按日期排序?因此,用户将更方便地查看新图像 代码如下:Php 如何按修改日期对图像进行排序?,php,Php,此代码按字母顺序对图像进行排序。如何按日期排序?因此,用户将更方便地查看新图像 代码如下: <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-swi
<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>
-
所有皮肤
-
该函数返回文件上次修改日期的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;
//检索所有图像,但不要浪费时间按字母顺序排列
$images=scandir($skindir,scandir\u SORT\u NONE);
//删除所有不存在的文件和非PNG文件
$images=数组过滤器($images,
函数($file)使用($skindir){
$path=“$skindir/$file”;
返回文件_exists($path)&&strtolower(路径信息($path,路径信息_扩展名))==“png”;
}
);
//按修改时间按降序对图像排序
usort($images,function($a,$b){returnfilemtime($a)-filemtime($b);});
//现在,您可以遍历图像并打印它们
foreach($curimg美元图像):?>
不工作的可能重复。没有错误,图像和标题不显示。@您为什么进行此编辑?在操作代码中(实际上是以错误的顺序显示图像),pathinfo函数被馈送给$images
的每个成员。我就是这样doing@TopoR请现在再试。我添加了代码,下面是它在@belletJuice的工作原理,因为scandir()
只返回文件(和目录)名称列表;如果只传递名称,则文件\u exists()
和路径信息()
函数将不会查看扫描的目录。我怀疑OP的代码“起作用”只是偶然或巧合,如果它真的起作用的话。