Page 1 of 1

Adding to a File NOT Overwriting...

Posted: Mon Dec 27, 2004 6:39 am
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

Posted: Mon Dec 27, 2004 7:22 am
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).

Posted: Mon Dec 27, 2004 8:55 am
by panoramical
Stupid question...how do you do that exactly?

Posted: Mon Dec 27, 2004 9:27 am
by feyd
[php_man]fopen()[/php_man]

Posted: Mon Dec 27, 2004 9:54 am
by panoramical
I meant reading from top/bottom.
But it doesn't matter anyway, now, because I've got it sorted.

Posted: Mon Dec 27, 2004 2:58 pm
by protokol
[php_man]fseek[/php_man]()