[SOLVED]Error with fclose()

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
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

[SOLVED]Error with fclose()

Post by idevlin »

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);
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]
Last edited by idevlin on Tue Jun 26, 2007 2:22 pm, edited 1 time in total.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

The problem is here:
idevlin wrote:

Code: Select all

$fp2=fwrite($fp2,$xml_output);
fwrite() returns an integer that shows you success, not the file handle.
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Post by idevlin »

superdezign wrote:The problem is here:
idevlin wrote:

Code: Select all

$fp2=fwrite($fp2,$xml_output);
fwrite() returns an integer that shows you success, not the file handle.
Bah, stupid me, been starting at the code too long. Well I'll check that. However the operation does work because the file is written correctly.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

It's not an issue of the file being written, it's an issue of you overwriting your file handle with an integer.
User avatar
idevlin
Forum Commoner
Posts: 78
Joined: Tue Jun 26, 2007 1:10 pm
Location: Cambridge, UK

Post by idevlin »

superdezign wrote:It's not an issue of the file being written, it's an issue of you overwriting your file handle with an integer.
Oh my God :oops: it's been a long day.....

Thanks
Post Reply