2013-10-12 80 views
0

我最近在另一個論壇上發現了這個PHP腳本 - 它應該將數組中的所有文件從最新到最舊的數組放到指定的目錄中,然後通過數組[0]返回最新的文件。列出過去24小時內添加的所有文件

是否有任何方法可以應用此腳本來獲取過去24小時內的所有文件?

在此先感謝您的幫助,這裏是代碼:

<?php 
$path = "docs/"; 
// show the most recent file 
echo "Most recent file is: ".getNewestFN($path); 

// Returns the name of the newest file 
// (My_name YYYY-MM-DD HHMMSS.inf) 
function getNewestFN ($path) { 
// store all .inf names in array 
$p = opendir($path); 
while (false !== ($file = readdir($p))) { 
if (strstr($file,".inf")) 
$list[]=date("YmdHis ", filemtime($path.$file)).$path.$file; 
} 
// sort array descending 
rsort($list); 
// return newest file name 
return $list[0]; 
} 
?> 

回答

2

用途:

print_r(get_24h_files('docs/')); 

功能:

function get_24h_files($dir) { 
    $iterator = new DirectoryIterator($dir); 
    $before_24h = strtotime('-24 hour'); 
    $files = array(); 
    foreach ($iterator as $fileinfo) { 
     if ($fileinfo->isFile() && $fileinfo->getMTime() >= $before_24h) { 
      $files[] = $fileinfo->getFilename(); 
     } 
    } 
    return $files; 
} 

附:如果您只需要.inf擴展名,請將$fileinfo->getExtension() == 'inf'添加到if語句中。

+1

這就像一個魅力!非常感謝 :) – Albab

相關問題