Programster's Blog

Tutorials focusing on Linux, programming, and open-source

Ubuntu 16.04 - Install PHP Inotify

PHP

Steps

Install inotify through PECL with the following commands:

sudo apt update
sudo apt install php-pear php7.0-dev -y
sudo pecl install inotify

Run these commands to add the extension to the PHP CLI:

SEARCH=";   extension=msql.so"
REPLACE=";   extension=msql.so\nextension=inotify.so"
FILEPATH="/etc/php/7.0/cli/php.ini"
sudo sed -i "s?$SEARCH?$REPLACE?" $FILEPATH

Run these commands to add the extension to PHP for Apache:

SEARCH=";   extension=msql.so"
REPLACE=";   extension=msql.so\nextension=inotify.so"
FILEPATH="/etc/php/7.0/apache2/php.ini"
sudo sed -i "s?$SEARCH?$REPLACE?" $FILEPATH

Helpful Starter Script

Below is a useful script to get you started with using inotify. You can use it to figure out what events get fired when you perform different actions.

#!/usr/local/bin/php
<?php

// Open an inotify instance
$inoInst = inotify_init();

// this is needed so inotify_read while operate in non blocking mode
stream_set_blocking($inoInst, 0);

// watch for everything in our input-bucket folder
$inputBucketWatcher = inotify_add_watch($inoInst, 'input-bucket', IN_ALL_EVENTS);

// add a secton watch to another directory, but only watch out create and delete.
$outputBucketWatcher = inotify_add_watch($inoInst, 'output-bucket', IN_CREATE | IN_DELETE);

// not the best way but sufficient for this example :-)
while (true)
{
    // read events
    $events = inotify_read($inoInst);

    if ($events[0]['wd'] === $inputBucketWatcher)
    {
        if ($events[0]['mask'] === IN_CREATE)
        {
            printf("Created file: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_DELETE)
        {
            printf("Deleted file: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_ACCESS)
        {
            printf("Accessed file: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_MODIFY)
        {
            printf("Modified file: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_ATTRIB)
        {
            printf("Metadata changed. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_CLOSE_WRITE)
        {
            printf("File opened for writing was closed. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_CLOSE_NOWRITE)
        {
            printf("File not opened for writing was closed. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_OPEN)
        {
            printf("File opened. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_MOVED_TO)
        {
            printf("File moved into directory. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_MOVED_FROM)
        {
            printf("File moved out of directory. file: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_DELETE_SELF)
        {
            printf("Watched directory deleted. file: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_MOVE_SELF)
        {
            printf("Watched directory moved. file: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_CLOSE)
        {
            printf("File closed. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_MOVE)
        {
            printf("File moved. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_ALL_EVENTS)
        {
            printf("IN_ALL_EVENTS activated. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_UNMOUNT)
        {
            printf("File system containing watched object was unmounted. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_Q_OVERFLOW)
        {
            printf("Event queue overflowed. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_IGNORED)
        {
            printf("Watch was removed. File: %s in watch_dir\n", $events[0]['name']);
        }

        if ($events[0]['mask'] === IN_ISDIR)
        {
            printf("Subject of this event is a directory. File: %s in watch_dir\n", $events[0]['name']);
        }
    }
    elseif ($events[0]['wd'] === $outputBucketWatcher)
    {
        // do something with other folder being watched.
    }
}

// stop watching our directories
inotify_rm_watch($inoInst, $inputBucketWatcher);
inotify_rm_watch($inoInst, $outputBucketWatcher);

// close our inotify instance
fclose($inoInst);

References

Last updated: 26th February 2022
First published: 16th August 2018

This blog is created by Stuart Page

I'm a freelance web developer and technology consultant based in Surrey, UK, with over 10 years experience in web development, DevOps, Linux Administration, and IT solutions.

Need support with your infrastructure or web services?

Get in touch