我想写一个PHP脚本,它会告诉我今天创建了多少个文件夹(未修改一个!!)。用于监控文件夹创建的PHP脚本
Ex。假设给了路径(如c:\ Data),所以我的脚本必须连续检查 是否为任何文件夹的新条目提供路径。我已经使用http://php.net/manual/en/function.date-diff.php。但获取修改后的文件夹的结果。
我想写一个PHP脚本,它会告诉我今天创建了多少个文件夹(未修改一个!!)。用于监控文件夹创建的PHP脚本
Ex。假设给了路径(如c:\ Data),所以我的脚本必须连续检查 是否为任何文件夹的新条目提供路径。我已经使用http://php.net/manual/en/function.date-diff.php。但获取修改后的文件夹的结果。
您可能希望尝试使用cron像每隔一分钟一样启动脚本,并检查目录列表之间的差异(来自之前和当前我的意思),而不是日期。这不是一个完美的解决方案,但它会起作用。
检查目录用一阳指:
$dirs = array_filter(glob('*'), 'is_dir');
Quote from @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);
另一种解决办法是使用一个数据库。任何不在数据库中的文件夹都是新的。这确保不会捕获修改的文件夹。
fileatime返回上次访问时间(即修改时间)。当我们将$ referenceFile与$ dirsLocation进行比较时,if condirion变为true,并且总是说这个文件是新的。 – user2473178
Bingo究竟是什么样的方式才是正确的。 – user2473178
检查如何在这里找到目录:http://stackoverflow.com/questions/2524151/php-get-all-subdirectories-of-a-given-directory?answertab=active#tab-top 并比较目录数组与:[array_diff](http://php.net/manual/en/function.array-diff.php) –