Search code examples
phpstringtextfilemtime

How to get the content of a file when it changes in PHP


I am fairly new to php.

I have a text file on my computer which changes the content frequently. I want to get the content of the file as soon as it is changed and insert it to an html form. I wanted to use the filemtime function on php but I didn't know exactly how to do it. I did put a do while loop :

$oldtime=filemtime($filename);
$newtime = $oldtime;
do {
    sleep (1);
    $newtime = filemtime($filename);
    echo "old: $oldtime"."<br />"; 
    echo "new: $newtime";
}
while ($newtime == $oldtime);

but this will keep executing. I did also try to do some if statements but this also won't work on my side.

Can you please suggest any way to do this ? If there is any other functions that may help ?

thank you for looking at this thread


Solution

  • Your condition in the while is logically incorrect. You are doing an assignment

    $newtime = $oldtime
    

    it should be a comparison:

    $newtime == $oldtime