I have a simple form to sign up for a newsletter, all I want to do is amend a text file with a new email address on the next line, I found some code in a previous post it works fine for the first 3 entries then for some reason I get a blank entry in the $rows array then this gets added again and again with new entries so I end up with a text file with loads of blanks rows, any ideas where its going wrong??
Code:
<?
$file="news.txt";
$rows = file($file); //read the file into an array
array_push($rows, $_POST['email']);
$contents=implode("\n", $rows); //turn the array back into one long string... depending on OS \n may need some tweaking
$fh = fopen($file, 'w'); //open the file in a truncated mode
fwrite($fh, $contents); //write the contents out
fclose($fh); //always close your file handles!
echo "Thanks your details have been submitted";
?>