Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP New Line will not work

Hi I am trying to create some code that first reads the existing contents of the file in and then adds the new line of code on a new line but the code i am using just adds it on the new text on to the already existing line instead of the new line...

Here is the code i am using:

<?php

$id = $_GET['id'];

$userfile = "user1.txt";
$fo = fopen($userfile, 'r') or die("can't open favourites file");
$currentdata = fread($fo, filesize($userfile));
fclose($fo);
$fw = fopen($userfile, 'w') or die("can't open favourites file");
$currentprocessed = "$currentdata\n";
fwrite($fw, $currentprocessed);
fwrite($fw, $id);
fclose($fw);
?>

I have tried a whole range of different ideas but nothing has worked, any help would be appreciated.

like image 391
user364333 Avatar asked Sep 14 '26 10:09

user364333


1 Answers

Line endings per OS

Unix / Linux
\n

DOS / Windows
\r\n

Invalid
\r and \n\r


The value of PHP_EOL constant depends on the platform php is running on.
It doesn't detect the line-endings in the current file or anything magic.

like image 128
Bob Fanger Avatar answered Sep 17 '26 00:09

Bob Fanger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!