Watch changes in the file system using PHP

Overview

Watch changes in the file system using PHP

Latest Version on Packagist Tests GitHub Code Style Action Status Total Downloads

This package allows you to react to all kinds of changes in the file system.

Here's how you can run code when a new file gets added.

use Spatie\Watcher\Watch;

Watch::path($directory)
    ->onFileCreated(function (string $newFilePath) {
        // do something...
    })
    ->start();

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/file-system-watcher

In your project, you should have the JavaScript package chokidar installed. You can install it via npm

npm install chokidar

or Yarn

yarn add chokidar

Usage

Here's how you can start watching a directory and get notified of any changes.

start(); ">
use Spatie\Watcher\Watch;

Watch::path($directory)
    ->onAnyChange(function (string $type, string $path) {
        if ($type === Watch::EVENT_TYPE_FILE_CREATED) {
            echo "file {$path} was created";
        }
    })
    ->start();

You can pass as many directories as you like to path.

To start watching, call the start method.

Detected the type of change

The $type parameter of the closure you pass to onAnyChange can contain one of these values:

  • Watcher::EVENT_TYPE_FILE_CREATED: a file was created
  • Watcher::EVENT_TYPE_FILE_UPDATED: a file was updated
  • Watcher::EVENT_TYPE_FILE_DELETED: a file was deleted
  • Watcher::EVENT_TYPE_DIRECTORY_CREATED: a directory was created
  • Watcher::EVENT_TYPE_DIRECTORY_DELETED: a directory was deleted

Listening for specific events

To handle file systems events of a certain type, you can make use of dedicated functions. Here's how you would listen for file creations only.

use Spatie\Watcher\Watch;

Watch::path($directory)
    ->onFileCreated(function (string $newFilePath) {
        // do something...
    });

These are the related available methods:

  • onFileCreated(): accepts a closure that will get passed the new file path
  • onFileUpdated(): accepts a closure that will get passed the updated file path
  • onFileDeleted(): accepts a closure that will get passed the deleted file path
  • onDirectoryCreated(): accepts a closure that will get passed the created directory path
  • onDirectoryDeleted(): accepts a closure that will get passed the deleted directory path

Watching multiple paths

You can pass multiple paths to the paths method.

use Spatie\Watcher\Watch;

Watch::paths($directory, $anotherDirectory);

Performing multiple tasks

You can call onAnyChange, 'onFileCreated', ... multiple times. All given closures will be performed

use Spatie\Watcher\Watch;

Watch::path($directory)
    ->onFileCreated(function (string $newFilePath) {
        // do something on file creation...
    })
    ->onFileCreated(function (string $newFilePath) {
        // do something else on file creation...
    })
    ->onAnyChange(function (string $type, string $path) {
        // do something...
    })
    ->onAnyChange(function (string $type, string $path) {
        // do something else...
    })
    // ...

Stopping the watcher gracefully

By default, the watcher will continue indefinitely when started. To gracefully stop the watcher, you can call shouldContinue and pass it a closure. If the closure returns a falsy value, the watcher will stop. The given closure will be executed every 0.5 second.

use Spatie\Watcher\Watch;

Watch::path($directory)
    ->shouldContinue(function () {
        // return true or false
    })
    // ...

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Parts of this package are inspired by Laravel Octane

License

The MIT License (MIT). Please see License File for more information.

Comments
  • How to start the supervisor?

    How to start the supervisor?

    Discussed in https://github.com/spatie/file-system-watcher/discussions/3

    Originally posted by albertopinilla January 11, 2022 Create a command in Laravel and it works without problems, the inconvenience occurs when I want to use the supervisor in Centos 7 and it generates the following error.

    This error occurs when I want to start the supervisor Screenshot 2022-01-11 205955

    In the log it shows this error Screenshot 2022-01-11 210117

    Supervisor settings Screenshot 2022-01-11 210257 .

    opened by albertopinilla 2
  • Add example Supervisord configuration so package can find Node.js

    Add example Supervisord configuration so package can find Node.js

    Since Supervisord is run via the system, it does not have access to the user's environment variables and cannot find node.js. The example script provides the way to do this in the Supervisord configuration's command parameter.

    opened by ryatkins 1
  • (new ExecutableFinder)->find('node') is empty when run through Supervisor or Cron

    (new ExecutableFinder)->find('node') is empty when run through Supervisor or Cron

    I'm trying to run this package through Supervisor or Cron and both cannot run because (new ExecutableFinder)->find('node') returns empty when setting up the getWatchProcess().

    Replacing with the system path for node /usr/local/bin/node solves the issue temporarily.

    Same issue as https://github.com/spatie/file-system-watcher/issues/4

    opened by ryatkins 1
  • Add php7 support

    Add php7 support

    Not so many features of php 8 was used and it also works with php 7 fundamentally, so I hope you accept this pull request :) I love php 8 too but it's a little too soon for it.

    opened by Stevemoretz 1
  • Could not get to work at all

    Could not get to work at all

    I could not get this lib to work at all.

    $watcher = \Spatie\Watcher\Watch::path($directory);
    $watcher->onAnyChange(function (string $type, string $path) {
        echo "{$path} was changed";
    })->start();
    

    I used the above code and could never get any kind of change or new file to trigger the callback.

    opened by joeworkman 1
Releases(1.1.1)
  • 1.1.1(Jun 16, 2022)

    What's Changed

    • Add example Supervisord configuration so package can find Node.js by @ryatkins in https://github.com/spatie/file-system-watcher/pull/12
    • Fixed getWatchProcess by @joeworkman in https://github.com/spatie/file-system-watcher/pull/2

    New Contributors

    • @ryatkins made their first contribution in https://github.com/spatie/file-system-watcher/pull/12
    • @joeworkman made their first contribution in https://github.com/spatie/file-system-watcher/pull/2

    Full Changelog: https://github.com/spatie/file-system-watcher/compare/1.1.0...1.1.1

    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Feb 17, 2022)

    What's Changed

    • Add support for configuration of timer by @Stevemoretz in https://github.com/spatie/file-system-watcher/pull/8

    New Contributors

    • @Stevemoretz made their first contribution in https://github.com/spatie/file-system-watcher/pull/8

    Full Changelog: https://github.com/spatie/file-system-watcher/compare/1.0.1...1.1.0

    Source code(tar.gz)
    Source code(zip)
  • 1.0.1(Feb 15, 2022)

    What's Changed

    • Update composer.json by @WyattCast44 in https://github.com/spatie/file-system-watcher/pull/5

    New Contributors

    • @WyattCast44 made their first contribution in https://github.com/spatie/file-system-watcher/pull/5

    Full Changelog: https://github.com/spatie/file-system-watcher/compare/1.0.0...1.0.1

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(May 6, 2021)

  • 0.0.3(May 5, 2021)

  • 0.0.2(May 5, 2021)

  • 0.0.1(May 4, 2021)

Owner
Spatie
We create products and courses for the developer community
Spatie
FIle Uploader is a php package to aid fast , easy and safe file upload

FILE UPLOADER FIle Uploader is a php package to aid fast , easy and safe file upload installation composer require codad5/file-helper Features Fast an

Aniezeofor Chibueze Michael 2 Sep 3, 2022
Media gallery with CKEditor, TinyMCE and Summernote support. Built on Laravel file system.

Documents ・ Installation ・ Integration ・ Config ・ Customization ・ Events ・ Upgrade ・ Demo ・ FAQ Features File upload and management Uploading validati

UniSharp 1.9k Jan 1, 2023
FileNamingResolver - A lightweight library which helps to resolve a file/directory naming of uploaded files using various naming strategies

FileNamingResolver - A lightweight library which helps to resolve a file/directory naming of uploaded files using various naming strategies

Victor Bocharsky 111 May 19, 2022
File manager module for the Lumen PHP framework.

Lumen File Manager File manager module for the Lumen PHP framework. Please note that this module is still under active development. NOTE: Branch 5.1 i

Digia 40 Aug 20, 2022
Upload File Library For PHP ( Simple & Easy User )

Upload File Library For Backend/ServerSide PHP ( Simple & Easy For Use ), Support Multiple Upload

Lamhot Simamora 1 Oct 12, 2021
Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP.

Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP. When you use this package, you're protected from vendor lock-in, That mean you can connect to any FTP/SFTP storage. Also it's comes with base URL option to connect Gotipath CDN.

Gotipath 2 Nov 3, 2021
PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats

PHPProject PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats, i.e. Micr

PHPOffice 192 Dec 17, 2022
ORM-based file upload package for php.

#Stapler Note: If you've previously been using this package, then you've been using it with Laravel. This package is no longer directly coupled to the

Code Sleeve 541 Dec 30, 2022
Detects file type by filename or content and generates correct mimetype.

FileTypeDetector Files type detector based on file name extension or file content (binary content). Usage Installation Supported formats Usage File Ty

Sergey 31 Oct 6, 2022
Uguu is a simple lightweight temporary file host with support for drop, paste, click and API uploading.

Uguu What is Uguu? Uguu is a simple lightweight temporary file hosting and sharing platform, but can also be used as a permanent file host. Features O

Eric Johansson (neku) 547 Dec 28, 2022
kodbox is a file manager for web. It is a newly designed product based on kodexplorer.

kodbox is a file manager for web. It is a newly designed product based on kodexplorer. It is also a web code editor, which allows you to develop websites directly within the web browser.You can run kodbox either online or locally,on Linux, Windows or Mac based platforms

warlee 1.2k Jan 7, 2023
A web based file manager,web IDE / browser based code editor

KodExplorer Update to kodbox: https://github.com/kalcaddle/kodbox Download | Demo It is recommended to use a new design upgrade product:kodbox 该项目处于维护

warlee 5.8k Jan 3, 2023
🛬🧾 A PHP8 TacView ACMI file format parser

A PHP8 TacView ACMI file format parser This package offers parsing support for TacView ACMI flight recordings version 2.1, it follows the standard des

Ignacio Muñoz Fernandez 1 Jan 18, 2022
Creating a file uploader with Laravel

File-Uploader File uploader is a simple group of restful APIs built in Laravel to upload file into a server and remove the file from that server. Tool

AmirH.Najafizadeh 4 Jul 31, 2022
Basic anonymous and registered upload storage for temporary share file self hosted.

TMPShareX Basic anonymous and registered upload storage for temporary share file self hosted. Server Requirement PHP 7.4.8 [Support PHP 8] Nginx 1.19.

Sandy Hermansyah 1 Feb 3, 2022
Simple file uploading, Pomf like

Pomf-0x80 Simple file uploading, Pomf like. Features One click uploading, no registration required IPFS Upload + Pin A minimal, modern web interface D

Iqbal Rifai 4 Apr 28, 2022
Private file storage and share with user build with laravel and vue inspired by google drive

LaravelDrive is a file storage system that allows store private file and share with users build wiht laravel and vue inspired by google drive. Laravel

Shahadat Hossain 70 Dec 22, 2022
A lightweight file manager with full ShareX, Screencloud support and more

XBackBone is a simple, self-hosted, lightweight PHP file manager that support the instant sharing tool ShareX and *NIX systems. It supports uploading

Sergio Brighenti 751 Jan 8, 2023
Pomf is a simple lightweight file host with support for drop, paste, click and API uploading.

Pomf Pomf is a simple file uploading and sharing platform. Features One click uploading, no registration required A minimal, modern web interface Drag

Pomf 772 Jan 8, 2023