PHP Classes

PHP iNotify Extension Class: Get notifications of when directory files change

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 135 All time: 9,286 This week: 488Up
Version License PHP version Categories
php-inotify 1.0.0MIT/X Consortium ...7.1Files and Folders, PHP 7
Description 

Author

This package can be used to get notifications of when directory files change.

It can register an object that is subscribed to file and directory change events using the PHP iNotify extension.

The package can also allow applications to consume the change events so they can be notified when files and directories of interest are changed.

Innovation Award
PHP Programming Innovation award nominee
August 2019
Number 9
inotify is the name of a PHP extension that is used to notify a PHP application when a given directory or file has its contents changed.

This package provides a wrapper around the inotify package, making it easier for PHP applications to keep track of changes done on any file or directory under operating systems supported by the inotify extension.

Manuel Lemos
Picture of Kacper Rowinski
  Performance   Level  
Name: Kacper Rowinski <contact>
Classes: 14 packages by
Country: Poland Poland
Age: 40
All time rank: 91426 in Poland Poland
Week rank: 180 Up1 in Poland Poland Up
Innovation award
Innovation award
Nominee: 8x

Example

<?php


use Inotify\InotifyConsumerFactory;
use
Inotify\InotifyEvent;
use
Inotify\InotifyEventCodeEnum;
use
Inotify\WatchedResourceCollection;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface;

include
__DIR__ . '/../vendor/autoload.php';

$inotifyConsumerFactory = new InotifyConsumerFactory();
$inotifyConsumerFactory->registerSubscriber(
    new class implements
EventSubscriberInterface
   
{
        public static function
getSubscribedEvents(): array
        {
            return [
InotifyEvent::class => 'onInotifyEvent'];
        }

        public function
onInotifyEvent(InotifyEvent $event): void
       
{
            echo
$event;
        }
    }
);
$inotifyConsumerFactory->consume(
   
WatchedResourceCollection::createSingle(
       
sys_get_temp_dir(),
       
// sys_get_temp_dir() . '/test.log',
        //InotifyEventCodeEnum::ON_CREATE()->getValue() | InotifyEventCodeEnum::ON_DELETE()->getValue(),
       
InotifyEventCodeEnum::ON_ALL_EVENTS()->getValue(),
       
'test'
   
)
);



Details

php-inotify

Why

In cases when you need to scan dir to find new files or files modifications, you probably will create some script and implements pulling mechanism. That is good for small systems with less files but not efficient enough for big one. And that why we got inotify mechanism that generate event on file|dir changes like create, delete, change and many more that we can listen to. More info in php manual. here

Installing inotify extension for php

To listen on event we need php extension called inotify. In most cases you just need to install using pecl

For example installation for php 7.1 on debian|ubuntu

apt-get update
apt-get install php-pear make php7.1-dev
pecl install inotify 
echo "extension=inotify.so" >>  /etc/php/7.1/cli/php.ini

Example

You can find example in example.php and events that you can listen to InotifyEventCodeEnum.php Event implement Arrayable, JsonSerializable and __toString.

Array
(
    [id] => 1
    [eventCode] => 256
    [eventDescription] => ON_CREATE - File or directory created in watched directory
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 32
    [eventDescription] => ON_OPEN - File was opened
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 4
    [eventDescription] => ON_ATTRIB - Metadata changed (e.g. permissions, mtime, etc.)
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 8
    [eventDescription] => ON_CLOSE_WRITE - File opened for writing was closed
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610455
)
Array
(
    [id] => 1
    [eventCode] => 512
    [eventDescription] => ON_DELETE - File or directory deleted in watched directory
    [uniqueId] => 0
    [fileName] => 2
    [pathName] => /tmp
    [customName] => test
    [pathWithFile] => /tmp/2
    [timestamp] => 1565610456
)

Where:

[id] => watch descriptor
[eventCode] => bit mask of events
[eventDescription] => human readable event description (can be UNKNOWN if not found in InotifyEventCodeEnum)
[uniqueId] => is a unique id to connect related events (e.g. IN_MOVE_FROM and IN_MOVE_TO)
[fileName] => name of a file (e.g. if a file was modified in a watched directory)
[pathName] => watched resource you give in configuration
[customName] => custom resource name for external parsing like "form-upload-dir" etc
[pathWithFile] => helper that contact pathName and fileName
[timestamp] => ...

  Files folder image Files (12)  
File Role Description
Files folder imageexample (1 file)
Files folder imagesrc (1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file ISSUE_TEMPLATE.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (12)  /  example  
File Role Description
  Accessible without login Plain text file example.php Example Class source

  Files folder image Files (12)  /  src  
File Role Description
Files folder imageInotify (7 files)

  Files folder image Files (12)  /  src  /  Inotify  
File Role Description
  Plain text file InotifyConsumerFactory.php Class Class source
  Plain text file InotifyEvent.php Class Class source
  Plain text file InotifyEventCodeEnum.php Class Class source
  Plain text file InotifyProxy.php Class Class source
  Plain text file InotifyProxyInterface.php Class Class source
  Plain text file WatchedResource.php Class Class source
  Plain text file WatchedResourceCollection.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:135
This week:0
All time:9,286
This week:488Up