append new content in a file from new line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vb_123
Forum Newbie
Posts: 19
Joined: Wed May 21, 2008 2:16 am

append new content in a file from new line

Post by vb_123 »

how to append new content in a file ??for example i have a file welcome.txt and i have some content in it lets say "hello world"...........now what i want is to append some more content in this existing file such that the new content comes in a new line........ like
hello world
welcome

thanks in advance
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: append new content in a file from new line

Post by VladSun »

Open it in "append" mode.

Code: Select all

$fh = fopen('test.txt', 'a');
There are 10 types of people in this world, those who understand binary and those who don't
vb_123
Forum Newbie
Posts: 19
Joined: Wed May 21, 2008 2:16 am

Re: append new content in a file from new line

Post by vb_123 »

i have already opened it in a append mode the content is getting appended but not from a new line.thats the main problem??
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: append new content in a file from new line

Post by VladSun »

Maybe because you have a "\n" caharacter when you write the string to the file.
Use trim() on the string before writing it.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply