I want to read a log file that is constantly being written to. It resides on the same server as the application. The catch is the file gets written to every few seconds, and
Yes, you need to sleep some time in the loop but you don't have to reopen the file. I was just looking for a similar problem. I wanted to read a file that might have been changed since last read.
The problem is that the resource has reached end of file (EOF
). And does not continue to read. The solution is to reset the pointer with fseek($fh, ftell($fh))
.
A complete program that waits for input in a text file might look like this one: