Feature #403 » monitor-poll.php
| 1 |
<?php
|
|---|---|
| 2 |
|
| 3 |
class watchpath { |
| 4 |
function __construct($path, $interval) { |
| 5 |
$this->path = $path; |
| 6 |
$this->interval = $interval; |
| 7 |
$this->last = time(); |
| 8 |
}
|
| 9 |
|
| 10 |
function wait() { |
| 11 |
while(true) { |
| 12 |
sleep($this->interval); |
| 13 |
|
| 14 |
clearstatcache(); |
| 15 |
if (FALSE == ($stat = stat($this->path))) { |
| 16 |
continue; |
| 17 |
}
|
| 18 |
|
| 19 |
$ctime = $stat[10]; |
| 20 |
|
| 21 |
if ($ctime <= $this->last) { |
| 22 |
continue; |
| 23 |
}
|
| 24 |
|
| 25 |
$this->last = $ctime; |
| 26 |
break; |
| 27 |
}
|
| 28 |
}
|
| 29 |
}
|
| 30 |
|
| 31 |
function printfile($path) |
| 32 |
{
|
| 33 |
$lines = file($path); |
| 34 |
echo("\n*** " . $path . "@" . $ctime . " ***\n"); |
| 35 |
foreach ($lines as $num => $line) { |
| 36 |
echo($line); |
| 37 |
}
|
| 38 |
}
|
| 39 |
|
| 40 |
|
| 41 |
$path = "/tmp/test.txt"; |
| 42 |
$seconds = 1; |
| 43 |
|
| 44 |
printfile($path); |
| 45 |
$watchdog = new watchpath($path, $seconds); |
| 46 |
|
| 47 |
while(true) { |
| 48 |
$watchdog->wait(); |
| 49 |
printfile($path); |
| 50 |
}
|
| 51 |
?>
|
| 52 |
|