Jump to content

Recommended Posts

I have a dynamic page that displays data from an XML. When the data is edited in the database the XML file is replaced with new content from the database. The problem is when the display page reloads (clicking refresh on the browser) the displayed contents do not change, even though the XML file definitely changed. When the XML file is missing, the PHP code behind the scenes makes the appropriate queries to the database to regenerate the XML file. So you would think that when I use FileZilla to go in manually and delete the XML file, then refresh the display page, I would see the latest data... but I don't. It is at least a few hours old and doesn't reflect the revised data which was changed only minutes before.

 

Here is the function that is used by the display page to get the XMLdata...

 

 

function utility_AirframeData_readFileCache($refAirframeSN)

{

$fileURL = "http://www.mywebsite.ca/AirframeDossierData.php?Serial=" . $refAirframeSN . ",Ignore=" . rand(0,10000);

$fileContents = file_get_contents($fileURL);

if( simplexml_load_string($fileContents) === FALSE )

{

utility_AirframeData_deleteFileCache($refAirframeSN);

$fileContents = file_get_contents($fileURL);

}

return new SimpleXMLElement($fileContents);

}

 

You can see that in an attempt to troubleshoot this I have added a parameter 'Ignore' which will be ignored by the URL, but will present a different URL to file_get_contents each time. This didn't work. So here is a bit about how AirframeDossierData.php... It looks to see if the XML file exists, and if it doesn't, makes sure it is built fresh from the data in the database. Either way once it is sure the XML file is in place it redirects to the XML file.

 

So file_get_contents asks for the contents of...

http://www.mywebsite.ca/AirframeDossierData.php?Serial=12345,Ignore=9876

...but instead gets directed to the XML file...

http://www.mywebsite.ca/Airframe/Data/000/000/012/0000012345.xml

...and it is supposed to open this file, and you would hope it opens the file as it is "now". So even when I delete the file and it is freshly regenerated, the file_get_contents presents old data. I have opened the XML file with a text editor and proven it is a new version and has new data. I added some debugging code to my display page to examine the data parsed by SimpleXMLElement, and it is showing the old content.

 

Has anyone seen anything like this before? Is there a way to force file_get_contents not to cache? I very much prefer the old cached data be in the XML file itself.

 

Thanks in advance for any wisdom you can share.

 

Regards,

 

Mike Henniger

Link to comment
https://forums.phpfreaks.com/topic/269415-file_get_contents-appears-to-be-caching/
Share on other sites

file_get_contents does not cache, it will request the file each time it is called. You may have something else that is caching sitting between your script and the URL you're fetching though, like a proxy server or cache server.

That was it! I accessed the file via a relative path rather than the URL and it work. I had set-up for the URL access for another purpose, but thought I could use it in this application as well. Apparently not. Lesson learned. Thanks!!!

 

Mike

Here is something I noticed as well, the URL (should be a file system path) you are reading isn't for the cache file, it's the actual web page.

 

The logic of your function should be -

 

<?php

function utility_AirframeData_readFileCache($refAirframeSN){
   $cache_file = some_function_that_returns_the_cache_file_name($refAirframeSN);
   if(!file_exists($cache_file)){
       // not in cache

       // code that reads the database, produces a fresh cache file, and supplies the xml data as a string

       $fileContents = the same data you just wrote to the cache file;

   } else {
       // in cache
       // code that reads the cache file and supplies the xml data as a string
       $fileContents = file_get_contents($cache_file);
   }
   return new SimpleXMLElement($fileContents);
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.