How To watch a file write in PHP?

前端 未结 6 1907
情书的邮戳
情书的邮戳 2020-12-05 15:29

I want to make movement such as the tail command with PHP, but how may watch append to the file?

6条回答
  •  一生所求
    2020-12-05 15:42

    Below is what I adapted from above. Call it periodically with an ajax call and append to your 'holder' (textarea)... Hope this helps... thank you to all of you who contribute to stackoverflow and other such forums!

    /* Used by the programming module to output debug.txt */
    session_start();
    $_SESSION['tailSize'] = filesize("./debugLog.txt");
    if($_SESSION['tailPrevSize'] == '' || $_SESSION['tailPrevSize'] > $_SESSION['tailSize'])
    {
          $_SESSION['tailPrevSize'] = $_SESSION['tailSize'];
    }
    $tailDiff = $_SESSION['tailSize'] - $_SESSION['tailPrevSize'];
    $_SESSION['tailPrevSize'] = $_SESSION['tailSize'];
    
    /* Include your own security checks (valid user, etc) if required here */
    
    if(!$valid_user) {
       echo "Invalid system mode for this page.";
    }
    $handle = popen("tail -c ".$tailDiff." ./debugLog.txt 2>&1", 'r');
    while(!feof($handle)) {
        $buffer = fgets($handle);
        echo "$buffer";
        flush(); 
    }
    pclose($handle);
    

提交回复
热议问题