White White - 1 year ago 70
PHP Question

how can i add email in one by one using read/write in php

how can i add email in one by one using read/write in php

Am getting the following output and create one folder called "update" update folder contain user entered one email is stored and user enter another email id already existing email id replaced to new email id why?

I need one by one email id called


here are my code please review

<form action="demo.php" method="post">
<input type="text" name="textEmail">
<input type="submit" value="send">

Demo.php file are


// Open the text file
$f = fopen("update.txt", "w");

// Write text

$text = strtr(" ",'&nbsp;', $_POST['textEmail']);

// Close the text file

// Open file for reading, and read the line
$f = fopen("update.txt", "r");

// Read text
echo fgets($f);


Answer Source

Open your file as append mode so that you not need to open the file twice, for writing in the file, for you one email per line you need to use the \n after each email. For reading use the while loop to read end of the file and use fgets to make sure it reads the whole line at a time.

$myfile = fopen("update.txt", "a+");

$txt = $_POST['textEmail']."\n";
fwrite($myfile, $txt);
while(!feof($myfile)) {
  echo fgets($myfile) . "<br/>";

Documentation: php_file_create and php_file_open

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download