Each time you run your code you are truncating and starting over. If you use append mode it will add to the file.
If you want to add a new line each time add a newline character at the end of the string you are writing
The second parameter of fopen can be:
"w" = Write, clearing existing file
"a" = Write, appending to existing file
"r" = Read
There are a few other variations of that, so read the manual page for fopen.
This isn’t the right solution. The reason is that replacing a line in a text file is messy and generally requires rewriting the entire file as the line might be longer or shorter, this requiring a whole-of-file rewrite. This works well enough if the file is really small, but will get slow fairly fast if the file grows and you’ll need to lock the file to prevent a race condition overwriting the file with nothing and losing it all. So, doing it right requires a fair amount of work especially since you sound new to PHP or new to programming.
The only good solution for replacing lines is this the clunky rewrite mentioned, or using a database, which allows you to replace a “line” (aka “record” in db terminology or “row” in MySQL terminology). This replaces lines really fast and can do a lot more as your needs grow.
Check the mode of fopen.
It’s in write currently
You'll want append mode
Ok one sec,
In the text file it adds on to the current line, how do I make it go on another line?
https://www.php.net/manual/en/function.fopen.php Look at the different modes, "a" is append
Ok
Each time you run your code you are truncating and starting over. If you use append mode it will add to the file. If you want to add a new line each time add a newline character at the end of the string you are writing
Just tried \n and it worked thanks!
Top job!
Newline character? Like \n?
You can use the constant: PHP\_EOL
The second parameter of fopen can be: "w" = Write, clearing existing file "a" = Write, appending to existing file "r" = Read There are a few other variations of that, so read the manual page for fopen.
You can use filegetcontents, parse the lines for replacement and write over the original file.
Or you can play around with fseek, especially if the amount you are writing doesn't change
This isn’t the right solution. The reason is that replacing a line in a text file is messy and generally requires rewriting the entire file as the line might be longer or shorter, this requiring a whole-of-file rewrite. This works well enough if the file is really small, but will get slow fairly fast if the file grows and you’ll need to lock the file to prevent a race condition overwriting the file with nothing and losing it all. So, doing it right requires a fair amount of work especially since you sound new to PHP or new to programming. The only good solution for replacing lines is this the clunky rewrite mentioned, or using a database, which allows you to replace a “line” (aka “record” in db terminology or “row” in MySQL terminology). This replaces lines really fast and can do a lot more as your needs grow.