abonnel-siteweb/data/pages/informatique/langage/php/simuler-tail-en-php.txt

44 lines
756 B
Plaintext

====== Simuler tail en PHP ======
{{ :dummy.png?75x75|Simuler tail en PHP}}
{{tag>php}}
<code PHP>
<?php
$handle = popen("tail -f /etc/httpd/logs/access.log 2>&1", 'r');
while(!feof($handle)) {
$buffer = fgets($handle);
echo "$buffer<br/>\n";
ob_flush();
flush();
}
pclose($handle);
</code>
<code PHP>
function follow($file)
{
$size = 0;
while (true) {
clearstatcache();
$currentSize = filesize($file);
if ($size == $currentSize) {
usleep(100);
continue;
}
$fh = fopen($file, "r");
fseek($fh, $size);
while ($d = fgets($fh)) {
echo $d;
}
fclose($fh);
$size = $currentSize;
}
}
follow("file.txt");
</code>