[SOLVED]fwrite problem

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
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

[SOLVED]fwrite problem

Post by rsmarsha »

I have the following code:

Code: Select all

$filename = 'clan/test.csv';
	if (file_exists($filename))
	{
		unlink($filename);
	}
	$label = fopen($filename, 'a') or die("Couldn't open file.");
	@fwrite($label, $label_string) or die("Couldn't write to file.");
	fclose($label);
This code is inside a while loop, so should write one line for each pass of the loop. For some reason it stops after one line. I know the loop is executing as the content on the page prints out correctly.
Last edited by rsmarsha on Thu Aug 31, 2006 7:09 am, edited 1 time in total.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

.. because you delete the file on every iteration perhaps?
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Lol i don't believe i missed that, lol. Heads spinning today, thanks for the tip. ;) :oops:

Sorted it now. :)

Code: Select all

$filename = 'clan/test.csv';
	if (file_exists($filename) && ($i==0))
	{
		unlink($filename);
	}
	$filename = 'clan/test.csv';
	$label = @fopen($filename, 'a+') or die("Couldn't open file.");
	@fwrite($label, $label_string) or die("Couldn't write to file.");
	fclose($label);
	$i++;
So it only deletes the file on the first loop, before writing the first line. :)
Post Reply