问题描述
我正在寻找一种使用 php 检测文件夹变化的解决方案。该应用程序可以在两个平台( linux 和 windows )上运行。只要结果相同,我可以对每个平台使用不同的方法。
我所希望的是:
I'm looking for a solution to detect changes in folder(s) using php. The application may run on both platforms(linux and windows). I may use different methods for each platform as long as results are the same.What I desire is :
- 如果将文件/文件夹添加到目录中,我希望我的应用能够检测到这个新的文件并读取其属性(
大小,文件时间
等) - 如果保存了现有文件/文件夹/内容已更改/删除,我需要检测到该文件已更改
- 如果我可以监视 apache 的webroot之外的基本文件夹(例如
c :\tmp
或Windows上的d:\音乐
或/ home / ertunc
在Linux上)
- If a file/folder is added to a directory, I want my app to detect this new file and read its attributes (
size,filetime
etc) - If a existing file/folder is saved/contents changed/deleted, I need to detect this file is changed
- It would be better if I can monitor a base folder outside webroot of apache (such as
c:\tmp
, ord:\music
on windows or/home/ertunc
on linux)
我在 inotify
上读过一些东西,但我不是确保它满足我的需求。
I read something on inotify
but I'm not sure it meets my needs.
推荐答案
因此,如果您要检查的是上次检查的结果,而不是立即进行更新更改后,您可以执行以下操作。
So if you are checking compared to the last time you checked rather than just being updated as soon as it changes you could do the following.
您可以创建目录的MD5,将其存储在此MD5中,然后将新的MD5与旧的MD5进行比较,以查看是否已更改
You could create an MD5 of a directory, storew this MD5 then compare the new MD5 with the old to see if things have changed.
以下函数取自 http://php.net/manual/en/function.md5-file.php
The function below taken from http://php.net/manual/en/function.md5-file.php would do this for you.
function MD5_DIR($dir)
{
if (!is_dir($dir))
{
return false;
}
$filemd5s = array();
$d = dir($dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..')
{
if (is_dir($dir.'/'.$entry))
{
$filemd5s[] = MD5_DIR($dir.'/'.$entry);
}
else
{
$filemd5s[] = md5_file($dir.'/'.$entry);
}
}
}
$d->close();
return md5(implode('', $filemd5s));
}
尽管这样效率很低,因为您可能知道,没有意义如果第一位不同,则检查目录的全部内容。
This is rather inefficient though, since as you probably know, there is no point checking the entire contents of a directory if the first bit is different.
这篇关于是否可以在Windows和Linux上使用php检测文件夹中的更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!