Php 列出过去24小时内添加的所有文件

Php 列出过去24小时内添加的所有文件,php,Php,我最近在另一个论坛上发现了这个PHP脚本——它应该将指定目录中的所有文件按从最新到最旧的顺序放入一个数组中,然后通过执行数组[0]返回最新的文件 是否有任何方法可以应用此脚本在过去24小时内获取所有文件 提前感谢您的帮助,以下是代码: <?php $path = "docs/"; // show the most recent file echo "Most recent file is: ".getNewestFN($path); // Returns the name of the

我最近在另一个论坛上发现了这个PHP脚本——它应该将指定目录中的所有文件按从最新到最旧的顺序放入一个数组中,然后通过执行数组[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;
}
功能:

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;
}
p、 如果您只需要
.inf
扩展名,请在
if
语句中添加
$fileinfo->getExtension()='inf'