informatique:langage:php: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");
  • informatique/langage/php/simuler-tail-en-php.txt
  • Dernière modification : 2023/02/02 08:45
  • de Cédric ABONNEL