Adding to a File NOT Overwriting...

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
panoramical
Forum Newbie
Posts: 8
Joined: Mon Dec 27, 2004 6:34 am

Adding to a File NOT Overwriting...

Post by panoramical »

Hello,
In my script I am using r+ to read the file, and the write to it also. However, this places the pointer at the start of the file, so it overwrites anything that overlaps the new content that is being written.
My question is this. Say we have a document like this, in a txt format:

Code: Select all

Apple Orange Pear Grape
Ford Citroen TVR Ferrari
And the user fills in a form that will upload this to that txt format:

Code: Select all

Pepsi Fanta Sprite 7-Up
How do I make it so that when the new data is written, the final result is this:

Code: Select all

Pepsi Fanta Sprite 7-Up
Apple Orange Pear Grape
Ford Citroen TVR Ferrari
All I need is to know what mode to read/write in, and what I need to actually write. For example, making a copy of the file, and including it in the writing string (I tried this, but no results.).
Thanks
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Look at using a+ instead of r+. That will append the line to the bottom of the file. And then, instead of reading the file from top to bottom, you simply read the file in, and reverse the line order (using the file function).
panoramical
Forum Newbie
Posts: 8
Joined: Mon Dec 27, 2004 6:34 am

Post by panoramical »

Stupid question...how do you do that exactly?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]fopen()[/php_man]
panoramical
Forum Newbie
Posts: 8
Joined: Mon Dec 27, 2004 6:34 am

Post by panoramical »

I meant reading from top/bottom.
But it doesn't matter anyway, now, because I've got it sorted.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

[php_man]fseek[/php_man]()
Post Reply