4

How could one write a new line to a file in php without erasing all the other contents of the file?

<?php if(isset($_POST['songName'])){ $newLine = "\n"; $songName = $_POST['songName']; $filename = fopen('song_name.txt', "wb"); fwrite($filename, $songName.$newLine); fclose($filename); }; ?> 

This is what the file looks like Current view

This is what is should look like Ideal View

5
  • Right now the content of the file looks like this 123456789 It should look like this 123 456 789 With each group of 3 numbers on a new line of the file Commented Aug 9, 2016 at 3:12
  • try using PHP_EOL instead of "\n" Commented Aug 9, 2016 at 3:14
  • Please add the details to your answer, within a code block, so we can see where the line breaks are and where they should be. This comment also doesn't tell us what was on the file, and what you're trying to append. Commented Aug 9, 2016 at 3:14
  • @coder that was exactly what I'm looking for thank you Commented Aug 9, 2016 at 3:18
  • glad i could help. stackoverflow.com/questions/3066421/… Commented Aug 9, 2016 at 3:19

3 Answers 3

5

Simply:

file_put_contents($filename,$songName.$newLine,FILE_APPEND); 

Takes care of opening, writing to, and closing the file. It will even create the file if needed! (see docs)

If your new lines aren't working, the issue is with your $newLine variable, not the file append operations. One of the following will work:

$newLine = PHP_EOL; << or >> $newLine = "\r\n"; 
Sign up to request clarification or add additional context in comments.

6 Comments

This does not add a new line to the file, it merely appends on to the end of the current line
@JohnSmith All file append operations, including the other solution, do the same thing. it appends whatever you indicate. If you want a new line, start with a new line character :-). Eg: append $newLine.$songName.$newLine
@JohnSmith I think it would help if you showed what you're getting, and what you expect to get. Your last comment under the other answer seems to be different from the OP. Do you want to add just one line (as in the OP), or add a new line at the end of each existing line (as in your comment)?
I attached images of the desired results to be displayed by the file
That works now, thank you for your help, it is very much appreciated! I Marked yours as the answer!
|
4

You have it set for writing with the option w which erases the data.

You need to "append" the data like this:

$filename = fopen('song_name.txt', "a");

For a complete explanation of what all options do, read here.

5 Comments

when set to "a" it does not add new line to the file it merely adds to the end.
That's not what I am trying to accomplish. I am trying to write to a new line in the file without erasing the file's current data
At a specific position?
Sure you could say that. I'm trying to add a new line at the end of each line of input.
I attached images of the desired results to be displayed by the file
2

To add a new line to a file and append it, do the following

$songName = $_POST['songName']; $filename = fopen('song_name.txt', "a+"); fwrite($filename, $songName.PHP_EOL); fclose($filename); 

PHP_EOL will add a new line to the file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.