CodeIgniter Forums
[SOLVED] write_file() - How to insert a newline character? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: [SOLVED] write_file() - How to insert a newline character? (/showthread.php?tid=39629)



[SOLVED] write_file() - How to insert a newline character? - El Forum - 03-16-2011

[eluser]Alexandro Corrêa[/eluser]
Hi Everyone!

I need write some information to a text file. Each information should stay in one different line. When I insert '\n' it is printed as text, not a newline.

Code:
$log1 = "Info 1\n";
$log2 = "Info 2\n";

write_file('.\app\logs\envio.txt', $log1, 'a');
write_file('.\app\logs\envio.txt', $log2, 'a');

Using this, the file is written, but the result is in just one line, not two lines with the information. I'm breaking my head to find a solution but until now I couldn't...

Thanks for your help!
Alex.


[SOLVED] write_file() - How to insert a newline character? - El Forum - 03-16-2011

[eluser]_TubbZ_[/eluser]
I maybe wrong but on windows I think you have to do \r\n to create a newline in a file.

EDIT:

guessing from your variable names are you creating a log file? You maybe better off using CI's log_message() method.


[SOLVED] write_file() - How to insert a newline character? - El Forum - 03-16-2011

[eluser]Alexandro Corrêa[/eluser]
Thanks TubbZ! But \r\n don't work too... it seems that write_file consider all characters on variable as text-only.


[SOLVED] write_file() - How to insert a newline character? - El Forum - 03-16-2011

[eluser]Alexandro Corrêa[/eluser]
I found a solution! The code was right, I don't know why, but I've excluded the file 'envio.txt' and try to write the file again, then php created the new file and wrote the information correctly. Maybe the format of file created initially was the problem.