I have a program that put logs into a .txt file, updating them regularly (every few seconds).
I’d like my PHP script to check the file size of this .txt, and, if it changed, refresh the page.
This is what I wrote, not working as expected:
$filename = "log.txt";
if (file_exists($filename)) {
$dfile = filesize($filename);
}
echo $dfile;
$c=0;
while($c < 10) {
if ($dfile !== filesize($filename)) {header("Location:http://127.0.0.1/parser.php");}
else{usleep(2000000);}
}
As Sid Malani said, definitely the sort of thing you should use AJAX for. I doubt this sort of thing could be done without some sort of asynchronous polling. At least in a browser environment (because of the way browsers work), on the console it wouldn’t be hard at all.
Now, the simplest way to do it is just to have the page refresh itself every so many seconds with an HTML meta refresh regardless of whether or not the log file has actually changed.
If you know that the logs are updating every few seconds anyway, it doesn’t really matter if they have changed size or not, because you can bet that they have anyway.