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();
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.
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
Here's how you can start watching a directory and get notified of any changes.
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. Note that the start
method will never end. Any code after that will not be executed.
To make sure that the watcher keeps watching in production, monitor the script or command that starts it with something like Supervisord. See Supervisord example configuration below.
The $type
parameter of the closure you pass to onAnyChange
can contain one of these values:
Watcher::EVENT_TYPE_FILE_CREATED
: a file was createdWatcher::EVENT_TYPE_FILE_UPDATED
: a file was updatedWatcher::EVENT_TYPE_FILE_DELETED
: a file was deletedWatcher::EVENT_TYPE_DIRECTORY_CREATED
: a directory was createdWatcher::EVENT_TYPE_DIRECTORY_DELETED
: a directory was deleted
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 pathonFileUpdated()
: accepts a closure that will get passed the updated file pathonFileDeleted()
: accepts a closure that will get passed the deleted file pathonDirectoryCreated()
: accepts a closure that will get passed the created directory pathonDirectoryDeleted()
: accepts a closure that will get passed the deleted directory path
You can pass multiple paths to the paths
method.
use Spatie\Watcher\Watch;
Watch::paths($directory, $anotherDirectory);
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...
})
// ...
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
})
// ...
By default, the changes are tracked every 0.5 seconds, however you could change that.
use Spatie\Watcher\Watch;
Watch::path($directory)
->setIntervalTime(1000000) //unit is microsecond therefore -> 0.1s
// ...rest of your methods
Notice : there is no file watching based on polling going on.
composer test
Create a new Supervisord configuration to monitor a Laravel artisan command which calls the watcher. While using Supervisord, you must specicfy your Node.js and PHP executables in your command paramater: env PATH="/usr/local/bin"
for Node.js, the absolute path to PHP and your project's path.
[program:watch]
process_name=%(program_name)s
directory=/your/project
command=env PATH="/usr/local/bin" /absolute/path/to/php /your/project/artisan watch-for-files
autostart=true
autorestart=false
user=username
redirect_stderr=true
stdout_logfile=/your/project/storage/logs/watch.log
stopwaitsecs=3600
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
Parts of this package are inspired by Laravel Octane
The MIT License (MIT). Please see License File for more information.