[SOLVED]Error with fclose()
Posted: Tue Jun 26, 2007 1:21 pm
feyd | Please use
But I don't like leaving a file handle lying about.
So, am I doing this a daft way? Or is there some unique issue with closing files in PHP that everyone except me knows about?
Thanks in advance!
Ian
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
First of all greetings to you all, this is my first post here
I'm relatively new to PHP, but not to programming. However I'm having a small issue with fclose and thought that I would seek the help of experts, so here I am.
Basically I'm doing the following:
Opening an XML file
Parsing it
Adding a new entry into it
Rewriting the file
Closing it
I can't use the new fangled XML functions with PHP 5 as my host currently doesn't have that version installed.
When I initially open the file, I use:
$fp=fopen($filename,"r");
As I only want to read it initially and return the file pointer to the start of the file when it's open.
I then need to close it again so that I can re-open it with "w" instead so that this time it will clear the file contents.
This all works fine until I try to fclose() the file again, when it decides to give me an error stating that the passed in file handle is invalid. I find this odd since the same handle was used to open it (no error) and write to it (no error) and everything actually works. I even gave the file handle a different name.Code: Select all
/* Close the file */
fclose($fp);
/* Reopen the file for writing */
$fp2=fopen($filename,"w");
if (!$fp2) {
print "<p>Error - Unable to re-open file " . $filename . " for writing</p>";
return "";
}
/* Write out the data to the file */
$fp2=fwrite($fp2,$xml_output);
if (!$fp2) {
print "<p>Error - Unable to write to file " . $filename . "</p>";
return "";
}
/* Close the file */
fclose($fp2);So, am I doing this a daft way? Or is there some unique issue with closing files in PHP that everyone except me knows about?
Thanks in advance!
Ian
feyd | Please use
Code: Select all
,Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]