8

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 :

  1. 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)
  2. If a existing file/folder is saved/contents changed/deleted, I need to detect this file is changed
  3. It would be better if I can monitor a base folder outside webroot of apache (such as c:\tmp, or d:\music on windows or /home/ertunc on linux)

I read something on inotify but I'm not sure it meets my needs.

Ertunç
  • 819
  • 1
  • 9
  • 21

3 Answers3

4

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:

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • Thank yo for your answer also. It lead me to disregard on the fly detection, since it introduces many other techniques especially on Windows platform. I upvote anyway cause it helped. – Ertunç Oct 21 '12 at 21:41
2

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.

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
  • This may be useful just to detect the change event. I think I also need the approach mentioned by Panique to maintain the list of changes. Any glitches on my 3rd requirement? – Ertunç Oct 21 '12 at 21:37
  • 1
    Monitoring outside of webroot is easy enough with php since it is server side. Second requirement, instead of doing an md5 on the entire directory, do seperate md5 checks on each individual file, then just keep a directory structure similar in structure but just containing md5 sums. – Jon Taylor Oct 21 '12 at 21:39
1

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/

Sliq
  • 15,937
  • 27
  • 110
  • 143