Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
fixed spelling
Source Link
Emil H
  • 40.3k
  • 11
  • 81
  • 97

I don't believe that there's some magical way to do it. You just have to continouslycontinuously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache()clearstatcache() before outputting any data.

Here's a quick sample, that doesn't include any error handelinghandling:

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"); 

I don't believe that there's some magical way to do it. You just have to continously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data.

Here's a quick sample, that doesn't include any error handeling:

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"); 

I don't believe that there's some magical way to do it. You just have to continuously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data.

Here's a quick sample, that doesn't include any error handling:

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"); 
easier syntax
Source Link
yPhil
  • 8.4k
  • 5
  • 61
  • 92

I don't believe that there's some magical way to do it. You just have to continously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data.

Here's a quick sample, that doesn't include any error handeling:

<?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"); 

I don't believe that there's some magical way to do it. You just have to continously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data.

Here's a quick sample, that doesn't include any error handeling:

<?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"); 

I don't believe that there's some magical way to do it. You just have to continously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data.

Here's a quick sample, that doesn't include any error handeling:

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"); 
Source Link
Emil H
  • 40.3k
  • 11
  • 81
  • 97

I don't believe that there's some magical way to do it. You just have to continously poll the file size and output any new data. This is actually quite easy, and the only real thing to watch out for is that file sizes and other stat data is cached in php. The solution to this is to call clearstatcache() before outputting any data.

Here's a quick sample, that doesn't include any error handeling:

<?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");