[英]PHP: display most recent images from directory?
我有一個攝像頭服務器,將圖像FTP傳輸到Web服務器。 誰能建議我需要通過服務器公共根目錄(/ public_html)查看並顯示四個最新圖像的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 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.