Monitor Filesystem Changes in PHP

Discussion in 'PHP' started by pradeep, Dec 14, 2012.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    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:

    PHP:
    <?
    $download_dir '/home/pradeep/downloads';

    // init inotify
    $fd inotify_init();
    // add new watch for newly created files
    $watch_descriptor inotify_add_watch($fd$dirIN_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:

    PHP:
    <?
    $download_dir '/home/pradeep/downloads';

    // init inotify
    $fd inotify_init();

    // add new watch for newly created files
    $watch_descriptor inotify_add_watch($fd$dirIN_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$dirIN_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.
     
  2. marlonmark

    marlonmark New Member

    Joined:
    Dec 13, 2012
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Service
    Location:
    India
    Home Page:
    http://www.us.justdial.com
    Thank you sir thats the cool and the descriptive way to make me understand.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice