在php目录中显示最新图像
我已经看过了,但只能找到如何显示最新的图像,或者全部显示。 我需要显示5个最新的图像。谢谢 显示1个图像的当前代码为在php目录中显示最新图像,php,Php,我已经看过了,但只能找到如何显示最新的图像,或者全部显示。 我需要显示5个最新的图像。谢谢 显示1个图像的当前代码为 <?php $dir = 'images/other'; $base_url = '/images/other'; $newest_mtime = 0; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if (($file != '.') &a
<?php
$dir = 'images/other';
$base_url = '/images/other';
$newest_mtime = 0;
if ($handle = opendir($dir)) {
while (false !== ($file = readdir($handle))) {
if (($file != '.') && ($file != '..')) {
$mtime = filemtime("$dir/$file");
if ($mtime > $newest_mtime) {
$newest_mtime = $mtime;
$show_file = "$base_url/$file";
}
}
}
}
print '<img src="' .$show_file. '" alt="code">';
?>
这应该适合您: (首先,我用和获取所有文件,并按最后一次修改对它们进行排序。之后,我用和获取5个最新文件。最后,我简单地循环遍历它们并打印图像)
您尝试了什么吗?1)将图像名称加载到数组中。2) 按创建时间降序排序。3) 显示数组的前5个索引。这些步骤中的哪一部分给你带来了麻烦?世界上最新的5幅图像?@Rizier123是的,但不适用于超过1项。@kainaw我是php新手,所以我真的不知道该怎么做。效果非常好!谢谢@不客气!祝你有一个愉快的一天:D(仅供参考:下次使用,这样你会得到更少的沮丧和更快的响应)
<?php
$dir = "images/other";
$files = glob($dir . "/*.*");
usort($files, function($a, $b){
return (filemtime($a) < filemtime($b));
});
$files = array_slice($files, 0, 5);
foreach($files as $file)
echo "<img src='" . $file. "' alt='code'>";
?>