Force file_get_contents to get fresh copy of remote file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmoussa
Forum Newbie
Posts: 8
Joined: Wed Mar 18, 2009 8:43 am

Force file_get_contents to get fresh copy of remote file

Post 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?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Force file_get_contents to get fresh copy of remote file

Post 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.
mmoussa
Forum Newbie
Posts: 8
Joined: Wed Mar 18, 2009 8:43 am

Re: Force file_get_contents to get fresh copy of remote file

Post 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.
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Force file_get_contents to get fresh copy of remote file

Post by php_east »

i suppose you have tried clearstatcache
mmoussa
Forum Newbie
Posts: 8
Joined: Wed Mar 18, 2009 8:43 am

Re: Force file_get_contents to get fresh copy of remote file

Post 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:
User avatar
php_east
Forum Contributor
Posts: 453
Joined: Sun Feb 22, 2009 1:31 pm
Location: Far Far East.

Re: Force file_get_contents to get fresh copy of remote file

Post 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.
Post Reply