Go4Expert

Go4Expert (http://www.go4expert.com/)
-   PHP (http://www.go4expert.com/articles/php-tutorials/)
-   -   Monitor Filesystem Changes in PHP (http://www.go4expert.com/articles/monitor-filesystem-changes-php-t29348/)

pradeep 14Dec2012 09:54

Monitor Filesystem Changes in PHP
 
System tools & monitoring programs are often required to monitor a directory or file(s) for changes, not just content changes, it may even be attribute changes like permissions, timestamps etc. Inotify is being used for quite some time in C programming to achieve the same.

Here we will be looking at enabling inotify support for PHP, and look at a few code snippets at how about using it. The on criteria for for using inotify is that it'll only work on a Linux system.

Enabling/Installing Inotify



Inotify is available as a PECL package, first get PECL from http://pecl.php.net/ if you do not already have it. After doing that just issue the following command as a root user:

Code:

$ pecl install inotify


Nothing else needs to be done to make it work.

Using Inotify



Inotify can monitor for a variety of events like file open, close, new file, changes, etc. you can get the full list here http://php.net/manual/en/inotify.constants.php

Now let's look at a simple program to monitor a directory for newly created files, it's pretty simple, go through the code below:

Code: PHP

<?
$download_dir = '/home/pradeep/downloads';

// init inotify
$fd = inotify_init();
// add new watch for newly created files
$watch_descriptor = inotify_add_watch($fd, $dir, IN_CREATE);

// loop and wait for events
while (1)
{
    $events = inotify_read($fd);
    $filepath = $dir . $events['name'];

    print "New file downloaded ".$events['name']."\n";
}

// clean up
inotify_rm_watch($fd, $watch_descriptor);

fclose($fd);
?>


In many cases we might want to monitor more than one events, like in the above example we might want to monitor for file modification as well, so here's how to do that:

Code: PHP

<?
$download_dir = '/home/pradeep/downloads';

// init inotify
$fd = inotify_init();

// add new watch for newly created files
$watch_descriptor = inotify_add_watch($fd, $dir, IN_CREATE);
// add new watch for newly created files, using the IN_ADD mask tells inotify_add_watch
// to append to existing watch and not replace it
$watch_descriptor = inotify_add_watch($fd, $dir, IN_ADD | IN_MODIFY);

// loop and wait for events
while (1)
{
    $events = inotify_read($fd);
    $filepath = $dir . $events['name'];

    // now we'll also have to check for the type of event
    if($events['mask'] & IN_CREATE) {
        print "New file downloaded ".$events['name']."\n";
    }
    else if($events['mask'] & IN_MODIFY) {
        print "File modified ".$events['name']."\n";
    }

}

// clean up
inotify_rm_watch($fd, $watch_descriptor);

fclose($fd);
?>


Similarly you can also watch files for changes, I hope this article was helpful. Enjoy.

marlonmark 17Dec2012 12:58

Re: Monitor Filesystem Changes in PHP
 
Thank you sir thats the cool and the descriptive way to make me understand.


All times are GMT +5.5. The time now is 05:24.