php - 我已经看到使用 php 在文件夹中显示第一张图像的代码,即最新图像,现在我如何使代码显示第二张和第三张
问题描述
我希望此代码显示文件夹中的第二张图像。你可以给我看另一个可以显示第三张图片的代码,谢谢。请,您的代码将不胜感激。
like so
<?php
$dir = 'uploads/';
$base_url = 'http://localhost/zac/uploads/';
$newest_mtime = 0;
$show_file = 'BROKEN';
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="Image Title Here">';
?>
解决方案
这是从 localhost 获取目录文件的另一种方法。
$base_url = 'http://localhost/zac/uploads/';
$dir = 'uploads/';
$files = glob($dir."*.*");
usort( $files, function( $a, $b ) { return filemtime($b) - filemtime($a); } );
$i = 1;
foreach($files as $file) {
$remove_ext = substr($file, 0, strrpos($file, "."));
if($i <= 3){
echo '<img src="'.$base_url.$file.'" alt="'.$remove_ext.'" ></br>';
}
$i++;
}
推荐阅读
- java - Java中的二进制和十六进制负数
- python - 将 KeyCode 对象与 python 中的字符串进行比较
- windows - IRQL 下降时如何在 Windows 中触发软件中断?
- mysql - MySql 存储过程 out id 返回 null
- apache-calcite - Apache Calcite 查询 json 嵌套数据
- spring-boot - 如何在 Spring Boot (Java) REST TEMPLATE 调用中设置贝宝获取 POSTMAN 调用的访问令牌参数
- angular - 除非单击,否则引导程序 4 选项卡未激活
- python - 将多个值绘制为范围 - matplotlib
- dart - 设置堆栈布局小部件的高度
- java - 如何从 HttpServeletRequest 对象中检索 @RequestBody 注释的参数值?