To write to another text file by appending the data in codeigniter

1.9k Views Asked by At
$myFile = APPPATH.'/../log.txt';
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "New Stuff 1\n";
fwrite($fh, $stringData);
$stringData = "New Stuff 2\n";
fwrite($fh, $stringData);
fclose($fh);

Above shown is my code.
output :

New Stuff 1New Stuff 2  

I need New Stuff 2 in the second line

Also the newly written data should append to the existing data.How to fix the issue.

2

There are 2 best solutions below

1
zzlalani On

You need to use PHP Predefined Constant PHP_EOL

$stringData = "New Stuff 1" . PHP_EOL;
fwrite($fh, $stringData);
$stringData = "New Stuff 2" . PHP_EOL;
fwrite($fh, $stringData);

it will produce \r\n

0
rohitarora On

file_put_content('your.txt',"data\n". PHP_EOL,FILE_APPEND);

try this.by the way if you open file in browser will will not show you next line with "\n" try to see it using view page source.or use notepad++ to se it.