townclown Posted July 16, 2008 Share Posted July 16, 2008 Hi guys/gals Having a bit of an issue here. What I am doing is using AJAX to check for a file getting updated. Now before I get flamed.. the problem has nothing to do with the AJAX, but rather the PHP script that it accesses. I don't claim to know too much about PHP so this might be an obvious one, but I tried for a few hours before turning to PHPfreaks. Basically, i want the following code to loop for a defined period of time (10 seconds here was just for testing, it will be increased) and when it detects that the file has been modified (the SESSION['timestamp'] variable is initialized prior to running the AJAX call) it must then just echo the new timestamp. Now, in the event that the file is modified prior to this script being called(but after the session var being set), it detects the difference between the session var and the new modified time and the loop echo's that out no problem. The issue is that once this loop has started(even if run for 60seconds) if the time.txt file is modified during that time, it is not detecting it and i do not know why. The getTimestamp() function works correctly. Thanks in Advance for any help. Hope i was clear enough. If someone has an easier way of doing this(wait till a file's mod time changes and echo a notification) i am all ears Darren. session_start(); function getTimestamp() { $target = "time.txt"; return date("YmdGis", filemtime($target)); } for($i = 1; $i <= 10; $i++) { $modTimestamp = getTimestamp(); if ($modTimestamp > $_SESSION['timestamp']) { $_SESSION['timestamp'] = $modTimestamp; echo $modTimestamp; break; } sleep(1); } Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted July 16, 2008 Share Posted July 16, 2008 which file do u want to know if it is modified your methodology seems off Quote Link to comment Share on other sites More sharing options...
townclown Posted July 16, 2008 Author Share Posted July 16, 2008 the time.txt file (the getTimestamp function returns its modified time) Quote Link to comment Share on other sites More sharing options...
townclown Posted July 17, 2008 Author Share Posted July 17, 2008 Nevermind then.. solve it myself! clearstatcache() the initial modtime gets cached when the loop starts. Using clearstatcache() in the loop fixes this issue. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.