Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 926
Ertunç Avatar asked Oct 21 '12 21:10

Ertunç


People also ask

How do I monitor a folder for changes?

Track Events with Event Viewer To monitor changes to a folder, you need to open the Event Viewer. In Windows search box, type “Event Viewer” and open the tool from the result. Go to “Windows Logs” and then “Security”. This will open a list of the recent activities on the middle panel.

How do I see all PHP files in a specific folder?

PHP using scandir() to find folders in a directory The scandir function is an inbuilt function that returns an array of files and directories of a specific directory. It lists the files and directories present inside the path specified by the user.

How do I monitor a directory in Linux?

In Linux, we can use the inotify interface to monitor a directory or a file. We do this by adding a watch to the directory or file. When we add a watch to a file, we can monitor it. For example, we'll know when a process opens, modifies, reads closes, moves, or deletes the file.

How can access file from another folder in PHP?

To access the root begin your path with "/". If you want to go up one directory from where you are currently, e.g. from /x/a/ to /x/ you could use "../". If you want to go back up two directories (e.g. from /x/a/ to /) you could use "../../" (rather than ".../" which you mentioned).


2 Answers

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:

  • How to efficiently monitor a directory for changes on linux? (looks best at a quick glance)
  • Monitor Directory for Changes

For Windows:

  • How to monitor a folder for changes, and execute a command if it does, on Windows?
  • The second answer in How can I monitor a Windows directory for changes?
like image 186
Pekka Avatar answered Sep 27 '22 21:09

Pekka


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.

like image 24
Jon Taylor Avatar answered Sep 27 '22 21:09

Jon Taylor