Page 1 of 1

Force file_get_contents to get fresh copy of remote file

Posted: Mon Mar 23, 2009 4:04 pm
by mmoussa
I'm using file_get_contents to retrieve a SWF file on a remote server. The purpose of the script
is to compare the contents of the SWF on "Server A" to the contents of the SWF on "Server B".
If the files differ, the file gets copied from "Server A" to "Server B".

The problem is that the server running the PHP script appears to be retrieving a cached version
of the SWF file. That is, the PHP script says the files are the same, but if my neighbor or I view
the URLs in our browsers, the files are different.

This happens intermittently, and I'm not sure how to tell file_get_contents to pull a fresh, un-cached
SWF from "Server A", or how to clear any sort of cache the PHP server may be maintaining separately.

This happens for images as well.

Code: Select all

 
if ( file_get_contents($urlA) == file_get_contents($urlB) )
{
    return false; // no change detected in files
}
 
I am not bound to file_get_contents. I can use some other mechanism for retrieval if it will resolve
this problem.

Does anybody have any suggestions?

Re: Force file_get_contents to get fresh copy of remote file

Posted: Mon Mar 23, 2009 4:10 pm
by JAB Creations
Add an HTTP query that is based on the time. So instead of fetching "file.php" try fetching "file.php?20080323" or something along those lines. I used the date but you may want to mix it up a little bit. This should treat the file as if it will give you different content based on the fact that HTTP queries mean the file has to process the input and give a result different dependent on that input.

Re: Force file_get_contents to get fresh copy of remote file

Posted: Tue Mar 24, 2009 8:15 am
by mmoussa
That's the first thing I tried, but it didn't make a difference.

BTW these are not php files I'm retrieving. They're .jpg or .swf
extensions.

Re: Force file_get_contents to get fresh copy of remote file

Posted: Tue Mar 24, 2009 9:14 am
by php_east
i suppose you have tried clearstatcache

Re: Force file_get_contents to get fresh copy of remote file

Posted: Tue Mar 24, 2009 11:32 am
by mmoussa
php_east wrote:i suppose you have tried clearstatcache
No... I haven't.

clearstatcache() caches the information returned in the affected functions list (http://us3.php.net/manual/en/function.c ... tcache.php). file_get_contents() is not in the affected list, plus the files I am accessing
are remote files, not local.

That, and the stat cache is automatically cleared after script execution. I'm only accessing these files once
during script execution. The problem persists over the course of several hours and many executions later.

Thanks, though. :cry:

Re: Force file_get_contents to get fresh copy of remote file

Posted: Tue Mar 24, 2009 1:52 pm
by php_east
ok, was just trying to find a quick fix.

since your browsers load fine, you might want to load that file using cURL mimicking a browser, which should then get you the latest as like your browsers, as there is technically no difference between a CURL get and a browser get, http wise.