writing to a text file

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
Glenvern
Forum Newbie
Posts: 2
Joined: Wed Jun 26, 2013 8:27 am

writing to a text file

Post by Glenvern »

I'm trying to save email addresses to a text file - (not having a great deal of success)

****Form for getting the email addresses**********
<form action="submit.php" method="post">
<label for="email">Your E-Mail Address : </label><br><br>
<input type="text" name="email">
<input type="submit" value="Submit" />
</form>

***********Submit.PHP for entering data into text file***************
<?php
$fw = fopen("textfile.txt", 'w');
fwrite ($fw, $_POST['email']);
fclose($fw);
?>

**********emailadds.PHP for displaying text file data********************
<?php
$file = textfile.txt";
$f = fopen($file, "r");
while ( $line = fgets($f, 1000))
print $line;
?>

Problem 1 - Emails are not being entered into text file
(if I enter them manually).
Problem 2 they do not display
question - Why, I thought the code was ok (I'm not a PHP coder consequently I'm struggling)

Can anyone help please.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: writing to a text file

Post by Celauran »

There are a number of possible problems here. First, take a look at fopen modes. I'd guess a+ is probably a better choice. Next, note that fopen() returns false if the file could not be opened. Test for that. Check file permissions to ensure your web server can write to the file in question. For reading, why not use file_get_contents()? Finally, why are you writing this to a file? Would a database not be a better solution?
Glenvern
Forum Newbie
Posts: 2
Joined: Wed Jun 26, 2013 8:27 am

Re: writing to a text file

Post by Glenvern »

Thanx for the response Celauran

I think I have mange to get most of it working now just one problem left and that is when I display the content of the text file is displays in a straight line not as a list which is what I had hoped for.

As for using a database that does seem like another can of worms for me, I wouldn't have a clue where to start..
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: writing to a text file

Post by Celauran »

Append \n to each line when writing, use nl2br when reading.
litebearer
Forum Contributor
Posts: 194
Joined: Sat Mar 27, 2004 5:54 am

Re: writing to a text file

Post by litebearer »

Maybe this may help...

Code: Select all

<?php

/* =========== adding emails to the file ============= */

/* STEP 1 */
/* get the email address */
/* you should validate the address */
$email = $_POST['email'];

/* STEP 2 */
* add a line break to the email address */
$email = $email . "\n";

/* STEP 3 */
/* set your file name */
$file = "textfile.txt";

/* STEP 4 */
/* append the email to the end of the file */
file_put_contents($file, $email, FILE_APPEND | LOCK_EX);

/* =========== displaying the file contents ========== */

/* read the file into an array */
$lines = file($file);

/* set a counter */
$i=0;

/* loop thru the array displaying the emails */
while($i<count($lines)) {
  echo $lines[$i] . "<br>";
  $i ++;
}
?>
Post Reply