PHP: How to read a file live that is constantly being written to

后端 未结 5 728
逝去的感伤
逝去的感伤 2020-12-10 03:22

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

5条回答
  •  温柔的废话
    2020-12-10 03:55

    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:

提交回复
热议问题