Welcome Guest [Log In] [Register]
We hope you enjoy your visit.

You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
filemtime()
Topic Started: Jul 23 2009, 07:40 PM (467 Views)
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
I'm using AJAX to call a php script every 2.5 seconds. The PHP uses clearstatcache() and then filemtime() to see if the file has been modified since the last time it was called (2.5 seconds before). If it has been modified, it spits out the new file contents, otherwise it does nothing. The only problem is, the filemtime() isn't updating at all through each of the AJAX requests unless I refresh the page. What could be the problem?
Offline Profile Quote Post Goto Top
 
Fission
Member Avatar
SRS BSNS
[ *  *  *  *  * ]
If you're using jQuery's AJAX, have you set the cache option to false?
Offline Profile Quote Post Goto Top
 
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
I'm using .getScript which appends a timestamp at the end of the query string so it overrides any cache options. I've just realised, that if I wait about 15 seconds the filemtime is updated, but that is far too long. Is there some other cache that filemtime uses that I'm not aware of?
Offline Profile Quote Post Goto Top
 
Jory
Member Avatar


clearstatcache() only clears the cache on the PHP side. The operating system say also cache stat data for a while. You'd need to find out what OS the server is running and then check the documentation of that OS.
Some simple tests show that PHP updates the time correctly. What methods are you using to change the file?
Offline Profile Quote Post Goto Top
 
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
In the testing stage I was manually editing and saving the file via ftp connection. I then tried using touch() to no avail. I'm 99% sure the OS is linux as I can use functions which don't work on a windows server.
Offline Profile Quote Post Goto Top
 
Jory
Member Avatar


Do you have shell access? If so, the following:
Code:
 
<?php

header("content-type: text/plain");

clearstatcache();

echo "Data from last go:\n";
echo "\tcontent:\t".file_get_contents('test.txt')."\n";
echo "\tfilemtime:\t".strftime("%X",filemtime('test.txt'))."\n";

?>

and then on the commandline, run te following command:
Code:
 
[root@server01 filemtime]# date +%X > test.txt | php index.php

The result should be something like this:
Code:
 
Data from last go:
content: 04:32:41 PM

filemtime: 16:32:41

My locale prefers 24-hour notation, but obviously 16:00 and 04:00 PM are the same.

If the results aren't the same, contact your hosting provider to ask them whats going on, as its something with the distrobution or configuration - we really can't help you with that.
Offline Profile Quote Post Goto Top
 
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
Hmm, I don't have shell access (as far as I know) but I did do a bit of testing with your code. Now, whenever I update the test.txt file the filemtime that it echos is always different (and accurate) therefore it is updating.

OK, done some testing and for some reason it seems to work as it should - just not in the script I need it to.

http://zetabin.com/test2.php
http://zetabin.com/test3.php

Go to both of those sites (at the same time) and wait for 10 seconds. test3.php updates test.txt, test2.php echoes the filemtime. As you can see it outputs what anyone would expect (in pacific time I think). So from that I can safely say the server settings are correct, it just doesn't work in my script.

Code - test2.php
 
<?php

header("content-type: text/plain");
flush();
clearstatcache();
$c=0;
while(true){
$c++;
if($c==10){
break;
}
clearstatcache();
echo "\tfilemtime:\t".strftime("%X",filemtime('test.txt'))."\n";
sleep(1);
}

?>


Code - test3.php
 
<?php
$fh = fopen('test.txt','a');
$c=0;
while(true){
$c++;
if($c==10){
break;
}
fwrite($fh,'<br />test');
sleep(1);
}
fclose($fh);
?>
Offline Profile Quote Post Goto Top
 
Jory
Member Avatar


Ok, so the PHP side of things is working correctly? What happens if you surf to the URL of the script you're calling with AJAX in your browser. (So if AJAX calls http://example.com/check-file.php, go to http://example.com/check-file.php in your browser.)
Offline Profile Quote Post Goto Top
 
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
http://zetabin.com/ZetaBoards/shoutbox/?sID=1248381519&lm=1248450424
http://zetabin.com/ZetaBoards/shoutbox/test.php

the first one is what it spits out
the second one is just a test file which writes to the file in question. what should happen is, the main file takes the $_GET['lm'] from the first script and checks it against the filemtime of the file in question. If $_GET['lm'] is less than the filemtime I know that something has changed so it reads the file. It then spits out the file contents and also :

console.log($_GET['lm']);console.log('filemtime')

So if I update the URL by changing what's after &lm= to what was in the second console.log it should recheck to see if anything has changed. Nothing will change unless I call the test.php file which appends some text to the file in question. At that point the main file should spit out the new contents.

That's kinda hard to explain though :S .

The easiest way to check it is to go: http://zetabin.com/ZetaBoards/shoutbox/?sID=1248381519&lm=1248450782 . That's the last time it was modified. It should just look like it's loading forever as well, it's in an infinite loop until the file is modified. Then if you go http://zetabin.com/ZetaBoards/shoutbox/test.php the first website you went to should update, but it doesn't unless you refresh the page.

EDIT: I've restarted the code and it seems I have something working. I'll bump the topic if anything comes up.
Ok, new problem with the script. I have it properly set up, and the comet process works perfectly. I'm using it for a shoutbox, so instead of AJAXing every 5 seconds or so for auto refresh, it uses comet to use 1 AJAX call immediately. This opens up the URL which does the looping until the file has changed, and when it has changed it outputs the new shoutbox content and then recalls the AJAX to start the feed again. So instead of having to wait between 5 second intervals, this uses a recurring feed. The only problem with this is, the AJAX call just stops after 60 seconds, which means the feed just breaks completely. I have even used set_time_limit(0);.

EDIT: Hmm, doesn't look like I can do it this way unfortunately. For some reason it's eating up my bandwidth even though it doesn't spit any data out unless the file has updated ...
Edited by Viral., Jul 24 2009, 02:51 PM.
Offline Profile Quote Post Goto Top
 
Fission
Member Avatar
SRS BSNS
[ *  *  *  *  * ]
I'm so glad I looked up comet. This might be the solution to some problems I've been having with the slow speeds of period AJAX refreshes.


Edit: of course, instead of checking for a file this will check for changes in database entries.
Edited by Fission, Jul 24 2009, 02:55 PM.
Offline Profile Quote Post Goto Top
 
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
Yeah it seems really useful, except for the fact that's it just used up 200 MB of bandwidth in a couple of hours. I still have no idea why it's doing it though, as far as I know, bandwidth isn't used if nothing is output.
Offline Profile Quote Post Goto Top
 
Fission
Member Avatar
SRS BSNS
[ *  *  *  *  * ]
Have you tried using Firebug's net tab? Any new requests should show up there.
Offline Profile Quote Post Goto Top
 
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
Yeah I've tried, it's not showing the new requests even though I can see from the status bar that they are being called.
Offline Profile Quote Post Goto Top
 
Ethyr
Member Avatar
Light of a Goddess
[ *  *  * ]
Viral.
Jul 24 2009, 03:18 PM
Yeah it seems really useful, except for the fact that's it just used up 200 MB of bandwidth in a couple of hours. I still have no idea why it's doing it though, as far as I know, bandwidth isn't used if nothing is output.
Since it's set to recurring, it'll eat up the bandwidth. Even though your computer may have it cached, it's looking for new variations of that cash every second, so that's why it's eating your bandwidth.
Offline Profile Quote Post Goto Top
 
Viral.
Member Avatar
Member
[ *  *  *  *  *  * ]
From an explanation given to me from Jory about a month ago, I was told that the bandwidth is calculated on what is output on the page. So, if a PHP file is 20 KB, and only 10 KB is output, that's how much bandwidth would be used. This wasn't outputting anything.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
Go to Next Page
« Previous Topic · Programming and Scripting Chat · Next Topic »
Add Reply