Let's say that we have a program X that continuously write a file for example the /tmp/test.file
I have a script in PHP that serves this file to the client so that he can download/read it.
How i'm able to do this continuously as the data are being written in the /tmp/test.file?
If i use the below code
<?php
$fp = fopen("/tmp/test.file","r");
while(!feof($fp))
{
echo fread($fp,4096);
}
fclose($fp);
It instantly stops because it reads the whole file until EOF so it doesnt care if another process is writing at this file.
Thank you
As a suggestion, use a continues Ajax call (for example 5 seconds interval) to the PHP
script file, and get the file content via offset. You can store the current offset of file into SESSION
or COOKIE
.
You can get file offset by using PHP
's ftell function.
As PHP
's official document defines ftell()
:
ftell — Returns the current position of the file read/write pointer
Also, stream_get_contents() function might be useful as PHP
's official document defines it:
stream_get_contents — Reads remainder of a stream into a string
then, In each interval, append the response data to a HTML tag.