PHP:显示目录中最新的图像?
问题描述:
我有一台摄像机服务器将图像传输到网络服务器。任何人都可以建议我需要通过服务器的公共根目录(/ public_html)来查看PHP代码片段并显示四个最新的图像?PHP:显示目录中最新的图像?
我可以通过日期/时间告诉照相机服务器命名上传的图像,但是需要[例如。 image-021020102355.jpg
为2010年10月2日下午11:55创建的图像]
谢谢!
答
我已经放在一起,可以帮助你的东西。这段代码在服务器的根目录中显示最近的最新图像。
<?php
$images = glob('*.{gif,png,jpg,jpeg}', GLOB_BRACE); //formats to look for
$num_of_files = 4; //number of images to display
foreach($images as $image)
{
$num_of_files--;
if($num_of_files > -1) //this made me laugh when I wrote it
echo "<b>".$image."</b><br>Created on ".date('D, d M y H:i:s', filemtime($image)) ."<br><img src="."'".$image."'"."><br><br>" ; //display images
else
break;
}
?>
答
这应做到:
<?php
foreach (glob('*.jpg') as $f) {
# store the image name with the last modification time and imagename as a key
$list[filemtime($f) . '-' . $f] = $f;
}
$keys = array_keys($list);
sort($keys); # sort is oldest to newest,
echo $list[array_pop($keys)]; # Newest
echo $list[array_pop($keys)]; # 2nd newest
如果你可以让文件名YYYYMMDDHHMM.jpg排序()可以把他们在正确的顺序,这会工作:
<?php
foreach (glob('*.jpg') as $f) {
# store the image name
$list[] = $f;
}
sort($list); # sort is oldest to newest,
echo array_pop($list); # Newest
echo array_pop($list); # 2nd newest