2

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

Toto
  • 89,455
  • 62
  • 89
  • 125
Momo
  • 523
  • 3
  • 11
  • 23

4 Answers4

1

Switch your assignment to a comparison (= for ==) to correct the bug in your code.

There may be more of a problem here, though. Files are not the best of places to store information, when you want to know if it's changed.

Ideally, you'd put the information that's being saved in that file inside of a database, where you can use fun things like triggers to let you know if data has changed. Otherwise, your solution will be wasting clock cycles trying to test the file endlessly for changes, which isn't very efficient

blueberryfields
  • 45,910
  • 28
  • 89
  • 168
  • Thanks fir the help, I have the == on my script but even with it, the script will give a timeout because of the php maximum execution time. What alternative methods do you think i can use if there is no access to a database ? – Momo Sep 09 '11 at 12:01
0

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

$newtime = $oldtime

it should be a comparison:

$newtime == $oldtime
codaddict
  • 445,704
  • 82
  • 492
  • 529
0

It keeps executing because you are assigning rather than comparing.

This:

while ($newtime = $oldtime);

Should be this:

while ($newtime == $oldtime);
evan
  • 12,307
  • 7
  • 37
  • 51
0

Polling the file frequently is kind of a crappy way to publish updates.

Why not just have a php script at the end point of the HTTP request which formats the source data (optionally caching it, if the source file is remote). Or using inotify to trigger the generation of a static file?

symcbean
  • 47,736
  • 6
  • 59
  • 94