Please how to achieve that emails are added to the file one per line, not appending all on one line? my current code: // input one email email = $_POST['email']; $stuff = serialize($email); $handle = fopen('email_list.txt','w+'); fwrite($handle, $stuff); // print all emails $contents = unserialize(file_get_contents('email_list.txt')); echo $file_contents; PHP: i tried like: fwrite($handle, $stuff . "\n"); or $stuff = serialize($email) . "\n\r"; but none works
I'm not sure what benefit there is in serialising in the text document but this is what I'd do. // input one email $email = myValidateEmailFunction($_POST['email']); if ($email){ $handle = fopen('email_list.txt','w+'); fwrite($handle, $email . "\n"); } // print all emails $contents = file_get_contents('email_list.txt'); echo nl2br($file_contents); PHP: If you need to serialise then you need to unserialise on a line by line basis.
You can simply do it like this: // input one email $email = (isset($_POST['email']) ? $_POST['email'].PHP_EOL : ''); $handle = fopen('email_list.txt','a'); fwrite($handle,$email); // print all emails foreach (file('email_list.txt') as $key => $value) { echo $value.'<br>'; } PHP: