用于监控文件夹创建的PHP脚本


PHP Script for Monitoring Folder Creation

我想写一个PHP脚本,告诉我今天创建了多少文件夹(没有修改!!)。

Ex。假设如果给定了路径(如c:''Data),那么我的脚本必须不断检查如果它是任何文件夹的新条目,则提供路径。我用过http://php.net/manual/en/function.date-diff.php.但也得到了修改文件夹的结果。

来自@Alin Purcaru的报价:

使用filectime。对于Windows,它将返回创建时间,而对于Unix,则返回更改时间,这是您能得到的最好的时间,因为在Unix上没有创建时间(在大多数文件系统中)。

使用参考文件来比较文件的年龄,可以在使用数据库的同时检测新文件。

// Path to the reference file. 
// All files newer than this will be treated as new
$referenceFile="c:'Data'ref";
// Location to search for new folders
$dirsLocation="c:'Data'*";
// Get modification date of reference file
if (file_exists($referenceFile))
  $referenceTime = fileatime($referenceFile);
else 
  $referenceTime = 0;
// Compare each directory with the reference file
foreach(glob($dirsLocation, GLOB_ONLYDIR) as $dir) {
  if (filectime($dir) > $referenceTime)
    echo $dir . " is new!";
}
// Update modification date of the reference file
touch($referenceFile);

另一种解决方案可能是使用数据库。任何不在数据库中的文件夹都是新文件夹。这样可以确保不会捕获修改后的文件夹。

您可能想尝试每一分钟用cron启动一次脚本,并检查目录列表(我的意思是以前和现在的)之间的差异,而不是日期。这不是一个完美的解决方案,但它会起作用。

检查目录arays与:

$dirs = array_filter(glob('*'), 'is_dir');

稍后将它们与array_diff 进行比较