Is there a way to detect changes in a folder using php on both windows and linux?

半世苍凉 提交于 2019-12-04 12:51:24

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.

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.

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.

Pekka supports GoFundMonica

Monitoring the filesystem for changes is a task that should be solved outside PHP. It's not really built to do stuff like this.

There are ready-made tools on both platforms that can monitor file changes that could call a PHP file to do the further processing.

For Linux:

For Windows:

I would

  1. scan all folders/files and create an array of them,
  2. save this somewhere
  3. run this scan again later [to check if the array still looks the same].

As you have the entire data structure from "time 1" and "now", you can clearly see what has changed. To crawl through the directories, check this: http://www.evoluted.net/thinktank/web-development/php-directory-listing-script and this http://phpmaster.com/list-files-and-directories-with-php/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!