Jump to content

[SOLVED] looping + file access issue


townclown

Recommended Posts

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);	
}

Link to comment
https://forums.phpfreaks.com/topic/115116-solved-looping-file-access-issue/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.