Amir Amir - 2 months ago 6
PHP Question

Making a linebreak in text file

i want to save every username in newline
i want text file to look like this :


  • name1

  • name2



instead of this :

name1name2

PHP code :

<?php
$username= $_POST['username']
file_put_contents("users.txt",$username);
?>

Answer
<?php
$username = "\n".$_POST['username'];
file_put_contents("users.txt",$username, FILE_APPEND);
?>

since you seemingly write at the end of the existing file, you have to prepend a linebreak to it.

on a side note: you should always be very careful when processing unfiltered user data.