簡體   English   中英

列出過去24小時內添加的所有文件

[英]List all files added in last 24 hours

我最近在另一個論壇上發現了這個PHP腳本 - 它應該將所有文件放在一個數組中從最新到最舊的指定目錄中,然后通過執行array [0]返回最新的文件。

是否有任何方法可以應用此腳本來獲取最近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];
}
?>

采用:

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

ps如果只需要.inf擴展名,請將$fileinfo->getExtension() == 'inf'if語句中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM