Is there a way in PHP to keep the file open and process each line and subsequent lines?

I want to write a php script that keeps the apache_log file open and listens for the log and processes each log entry when this happens.

I think I need to open the file, get the number of lines, and then do it again in a loop - and when the size is different, read the new lines and process them.

Am I sneaking up on the wrong tree, or is there a silly simple solution I missed?

Chris

+3
source share
3 answers

A quick-dirty way would be to use it somehow tail -f(if available):

PHP: tail -f file | php myscript.php, php://stdin.

popen script:

$res = popen('tail -f file_name', 'r');

while (!feof($res)) {
  $line = fgets($res);
  echo $line;
}
+1

php inotify, , , . eralier , , .

0

The option that comes to my mind will run the script from the cron job every minute or so and just remember the last line that you were in every moment.

0
source

Source: https://habr.com/ru/post/1738072/


All Articles