PHP code example of lav45 / watcher

1. Go to this page and download the library: Download lav45/watcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lav45 / watcher example snippets


use Lav45\Watcher\Event;
use Lav45\Watcher\Listener;
use Lav45\Watcher\Watcher;

$watcher = (new Watcher(new Listener()))
    ->on(IN_CREATE | IN_MOVED_TO, static function(Event $event) {
        echo 'Create ' . $event->path . "\n";
    })
    ->on(IN_DELETE | IN_MOVED_FROM, static function(Event $event) {
        echo 'Delete ' . $event->path . "\n";
    })
    ->on(IN_MODIFY, static function(Event $event) {
        echo 'Update ' . $event->path . "\n";
    })
    ->withFilter(fn(Event $event): bool => \str_ends_with($event->path, '.json'))
    ->watchDirs([__DIR__]);

while (true) {
    $watcher->read();
    sleep(1);
}