This question already has an answer here:
I am trying to get the code below to write two names (form data) on separate lines. It shows up on separate lines if I open the .txt file in Atom, but it only shows up on one line in notepad.
<?php
error_reporting(E_ERROR | E_PARSE);
$nameOne = $_POST['nameOne'];
$nameTwo = $_POST['nameTwo'];
$newfile = fopen("newfile.txt", "w");
$txt = "$nameOne
";
fwrite($newfile, $txt);
$txt = "$nameTwo
";
fwrite($newfile, $txt);
fclose($newfile);
?>
</div>
use PHP_EOL
constant instead of or
since you are using Windows (
works well on Unix/Linux).
Use instead of
<?php
error_reporting(E_ERROR | E_PARSE);
$nameOne = $_POST['nameOne'];
$nameTwo = $_POST['nameTwo'];
$newfile = fopen("newfile.txt", "w");
$txt = "$nameOne
";
fwrite($newfile, $txt);
$txt = "$nameTwo
";
fwrite($newfile, $txt);
fclose($newfile);
?>
OR you can use PHP_EOL
<?php
error_reporting(E_ERROR | E_PARSE);
$nameOne = $_POST['nameOne'];
$nameTwo = $_POST['nameTwo'];
$newfile = fopen("newfile.txt", "w");
$txt = "$nameOne".PHP_EOL;
fwrite($newfile, $txt);
$txt = "$nameTwo".PHP_EOL;
fwrite($newfile, $txt);
fclose($newfile);
?>
Different operating systems use different codes. Windows is " "
, OSX is " "
, etc. Use the PHP_EOL
constant instead of either code for autodetection!
Go here: http://php.net/manual/en/function.fopen.php for more detail.