PHP overwrite file

Soldato
Joined
1 Feb 2006
Posts
8,188
Hi guys,

I have written a piece of code which will create an RSS file and export data into the file. I will be running this file on the server at set intervals such as every 15 minutes so therefore I need to create a file, then 15mins later overwrite the file with a new updated version from the database.

The following code is what I have so far:

Code:
$handle = fopen("$full_path", "x+");
			
$xml = "stuff goes here from database";
			
fwrite($handle, $xml);
fclose($handle);

How can I modify this code to ensure that if a file already exists on my webserver it will automatically be overwritten with the new file produced by this code?

Any help would be appreciated. Cheers
 
you don't need to save it to a file. write a php file that outputs the content formatted as xml with the header text/xml. There's no need to create a file, readers will update every so often and grab the new content because there's new rows in the database.

if you have to check whether a file exists:

Code:
if ([url=http://uk3.php.net/manual/en/function.file-exists.php]file_exists[/url]('path/to/file'))
{
  return false;
}
else
{
  return true;
}
 
Last edited:
Sic, it's often better to cache the data like this rather than generate it dynamically.

jonnyc747 said:
How can I modify this code to ensure that if a file already exists on my webserver it will automatically be overwritten with the new file produced by this code?


Open it in mode w, which will clear the file's contents before writing to it if it already exists, and create it if it doesn't.

Code:
$fh = fopen('file.txt', 'w');
fwrite($fh, 'foo');
fclose($fh);
 
i think it was the checking every 15 minutes bit that threw me. i guess if you just rewrote it every time something was posted, that'd make more sense than actually messing about generating it every time. If you've got loads of categories and stuff though, i'd tend to generate that dynamically.
 
hi guys, thanks a lot for the info. Using the 'w' mode worked a treat. Instead of using a cron tab to run every 15 mins is it possible to only run as soon as an update is executed on the database? Kind of like a trigger idea? Someone suggested the other day to me that MySQL doesnt support triggers but maybe there is another workout for checking the database.

Thanks again
 
Just following on from this guys, I have created links within my RSS feeds to go directly to pages in my site. As I am testing this on a localhost address, none of the functionality is enabled. I can view the page but no links work. Are there any workarounds for this? Or does anyone know will a newsreader application work with localhost addresses? Thanks
 
you could edit your hosts file so that all requests to the domain goto 127.0.0.1 - just add the line under localhost.

Code:
127.0.0.1	yourdomain.com

on windows you can open your hosts file by doing this, start, run

notepad %windir%\system32\drivers\etc\hosts

:)
 
sorry im a mac newb and i dont follow at all! have you any links to instructions? sorry for being a pain

edit: i am using mamp as my local testing platform if that means anything
 
Last edited:
jonnyc747 said:
edit: i am using mamp as my local testing platform if that means anything

nope, that doesn't mean a thing. :p

the hosts file is part of the operating system. im guessing rob has posted the path to the file above. you just need to browse there and edit the file in a text editor. :)
 
Back
Top Bottom