[thelist] PHP file_put_contents help

Pringle, Ron RPringle at aurora-il.org
Tue Jan 23 10:01:24 CST 2007


I have some code that grabs weather data from an XML file on the NOAA website and writes it to an XML file locally on my server. Actually it checks to see if the file already exists, and if it does, if it was created over an hour ago. If it doesn't exist or was created more than an hour ago, it grabs the file off the NOAA site and either writes it to a new file or overwrites the existing one. I then do an XSL transformation on it and spit it back out to the user later on.

On my current server, running IIS5 and PHP 5.1.0 as an ISAPI module, this all works fine. I have the $filename specified as just $filename = 'KARR.xml', as the file exists at the root level.

On the new server I'm about to deploy, running IIS6 and PHP 5.1.0 as an ISAPI module, I can check for the existence of the current file and get the creation date if it exists, but creating the file or writing data to the existing file is failing. Due to security concerns, I moved the file to a directory with the proper permissions on it, so now it is referred to as $filename = '//10.1.1.5/aurorasite/feeds/KARR.xml'.

After reading up some more on file_put_contents, it says it can take a URL as the file variable, but it appears that this might be the part that is failing. So I'm wondering if I'm specifying the path wrong (even though it works for checking if the file exists) somehow.

Any thoughts appreciated. Complete code is below:

// Get Weather Routine //
$filename = '//10.1.1.5/aurorasite/feeds/KARR.xml';
$weatherdata = 'http://www.nws.noaa.gov/data/current_obs/feeds/KARR.xml';

// check to see if the local file exists
if (file_exists($filename)) {
	echo "<p>File exists.</p>\n";
	// get difference in seconds between now and last modified date
	$diff = (time() - filemtime("$filename"))/60*60;
	echo "<p>File age in seconds: $diff</p>\n";
	// if greater than 1 hr (3600 seconds) get new file from source
	if ($diff >= 3600) {
		echo "<p>Its over 1 hour old. Attempting to obtain fresh data and write to file.</p>\n";
		file_put_contents($filename,fopen($weatherdata, "rb"), LOCK_EX);
		};
	} else {
		echo "<p>File does not exist. Attempting to create file.</p>\n";
		// file doesn't exist, get data and create new file //
		file_put_contents($filename,fopen($weatherdata, "rb"), LOCK_EX);
		//check again to see if local file was created properly
		if (file_exists($filename)) {
			echo "<p>File was successfully created.</p>\n";
		} else {
			echo "<p>File was not created.</p>\n";
		}
	};


Ron



More information about the thelist mailing list