Page 1 of 1

[SOLVED]fwrite problem

Posted: Thu Aug 31, 2006 6:52 am
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.

Posted: Thu Aug 31, 2006 6:57 am
by Jenk
.. because you delete the file on every iteration perhaps?

Posted: Thu Aug 31, 2006 6:59 am
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. :)