【发布时间】:2013-01-12 20:34:12
【问题描述】:
非常常见的问题,但仍然没有找到正确的解决方案。我需要每天早上运行 cron 作业来启动 php 脚本,以列出夜间在 Web 服务器上创建的所有新文件。这对于查看访问者在夜间上传的内容非常有用,而且这些文件通常可能是有害文件,会伤害其他访问者的计算机。到目前为止,我有这个:
$dir = "../root/";
$pattern = '\.*$'; // check only file with these ext.
$newstamp = 0;
$newname = "";
if ($handle = opendir($dir)) {
while (false !== ($fname = readdir($handle))) {
// Eliminate current directory, parent directory
if (ereg('^\.{1,2}$',$fname)) continue;
// Eliminate other pages not in pattern
if (! ereg($pattern,$fname)) continue;
$timedat = filemtime("$dir/$fname");
if ($timedat > $newstamp) {
$newstamp = $timedat;
$newname = $fname;
}
}
}
closedir ($handle);
// $newstamp is the time for the latest file
// $newname is the name of the latest file
// print last mod.file - format date as you like
print $newname . " - " . date( "Y/m/d", $newstamp);
这将打印最新的文件,但仅在一个目录 root/ 中,并且不检查例如 root/folder/ 等。如何重新执行? 如果我在根/文件夹中添加一个新文件,脚本将显示带有日期的文件夹,但不会显示在根/文件夹中创建了哪个文件。希望你明白我的意思,谢谢
【问题讨论】:
-
当用户上传文件时,我会更简单地进行检查。
-
当我们谈论 300-400 个文件时并不容易..