informatique:simuler-tail-en-php

Simuler tail en PHP

Simuler tail en 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);
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");
Entrer votre commentaire. La syntaxe wiki est autorisée:
  __  __  _      __  _   __   _  __   __ __
 / / / / | | /| / / | | / /  | |/_/  / // /
/ /_/ /  | |/ |/ /  | |/ /  _>  <   / _  / 
\____/   |__/|__/   |___/  /_/|_|  /_//_/
 
  • informatique/simuler-tail-en-php.txt
  • Dernière modification: 2020/12/19 15:47
  • de Cédric ABONNEL