ryanfilard Posted June 24, 2011 Share Posted June 24, 2011 I have to create a script that writes a user's email to a .txt file. I am using the code below and it does not seem to be working. <?PHP //Data to write $newsletterinfo = $REQUEST['email']; //Open the file directory $fdir = fopen('newsletter.txt'); //Write user email fwrite($fdir, $newsletterinfo); //Close the connection fclose($fdir); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240312-writing-a-string-to-a-file/ Share on other sites More sharing options...
fugix Posted June 24, 2011 Share Posted June 24, 2011 Ou forgot an underscore in your request array. Try $newsletterinfo = $_REQUEST['email']; Also, I prefer using file_put_contents() Quote Link to comment https://forums.phpfreaks.com/topic/240312-writing-a-string-to-a-file/#findComment-1234368 Share on other sites More sharing options...
ryanfilard Posted June 24, 2011 Author Share Posted June 24, 2011 I changed it but it still does not work. I am doing something wrong? <?PHP //Data to write $newsletterinfo = $_REQUEST['email']; //Open the file directory $fdir = fopen('newsletter.txt'); //Write The Data fwrite($fdir, $newsletterinfo); //Close the connection fclose($fdir); ?> Quote Link to comment https://forums.phpfreaks.com/topic/240312-writing-a-string-to-a-file/#findComment-1234373 Share on other sites More sharing options...
fugix Posted June 24, 2011 Share Posted June 24, 2011 Just noticed, in your fopen() function, you dud not specify a mode in which to open the file. Change it to. $fdir = fopen('newsletter.txt','w'); Quote Link to comment https://forums.phpfreaks.com/topic/240312-writing-a-string-to-a-file/#findComment-1234390 Share on other sites More sharing options...
ryanfilard Posted June 24, 2011 Author Share Posted June 24, 2011 When I add the data how do I make it so it does not do this: email@email.comemail@email.comemail@email.comemail@email.comemail@email.comemail@email.com I want it to do this: email@email.com email@email.com email@email.com Quote Link to comment https://forums.phpfreaks.com/topic/240312-writing-a-string-to-a-file/#findComment-1234400 Share on other sites More sharing options...
fugix Posted June 24, 2011 Share Posted June 24, 2011 fwrite($fdir, $newsletterinfo . "\n"); Quote Link to comment https://forums.phpfreaks.com/topic/240312-writing-a-string-to-a-file/#findComment-1234404 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.