forked from reactphp/filesystem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_tail.php
24 lines (20 loc) · 880 Bytes
/
file_tail.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
use React\EventLoop\Loop;
use React\EventLoop\TimerInterface;
use React\Filesystem\Factory;
use React\Filesystem\Node\FileInterface;
require 'vendor/autoload.php';
$filename = tempnam(sys_get_temp_dir(), 'reactphp-filesystem-file-tail-example-');
$offset = 0;
$file = Factory::create()->detect($filename)->then(function (FileInterface $file) {
Loop::addPeriodicTimer(1, function (TimerInterface $timer) use ($file, &$offset): void {
$file->getContents($offset)->then(function (string $contents) use (&$offset, $timer): void {
echo $contents;
$offset += strlen($contents);
if (trim($contents) === 'done') {
Loop::cancelTimer($timer);
}
});
});
})->done();
echo 'Append data to "', $filename, '" to see it appear beneath here, put "done" on a new line to stop watching it:', PHP_EOL;