简体   繁体   English

PHP:显示目录中的最新图像?

[英]PHP: display most recent images from directory?

I have a camera server FTPing images to a webserver. 我有一个摄像头服务器,将图像FTP传输到Web服务器。 Can anyone suggest the PHP snippet I'd need that would look through the server's public root directory (/public_html) and display the four most recent images? 谁能建议我需要通过服务器公共根目录(/ public_html)查看并显示四个最新图像的PHP代码段?

I can tell the camera server to name uploaded images by date/time, however needed [eg. 我可以告诉相机服务器按日期/时间命名上传的图像,但是需要[例如。 image-021020102355.jpg for an image created 2nd oct 2010 at 11:55pm] image-021020102355.jpg用于在2010年10月2日晚上11:55创建的图像]

thanks! 谢谢!

I've put together something that can help you. 我整理了一些可以帮助您的东西。 This piece of code displays the last recent images in the root directory of your server. 这段代码显示了服务器根目录中的最新图像。

<?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;
    }
?>

This should do it: 应该这样做:

<?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

If you can make the filenames YYYYMMDDHHMM.jpg sort() can put them in the right order and this would work: 如果可以使文件名YYYYMMDDHHMM.jpg sort()可以按正确的顺序放置它们,则可以使用:

<?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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM