2010-10-02 49 views
1

我有一臺攝像機服務器將圖像傳輸到網絡服務器。任何人都可以建議我需要通過服務器的公共根目錄(/ public_html)來查看PHP代碼片段並顯示四個最新的圖像?PHP:顯示目錄中最新的圖像?

我可以通過日期/時間告訴照相機服務器命名上傳的圖像,但是需要[例如。 image-021020102355.jpg爲2010年10月2日下午11:55創建的圖像]

謝謝!

回答

1

我已經放在一起,可以幫助你的東西。這段代碼在服務器的根目錄中顯示最近的最新圖像。

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

這應做到:

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