prepend a text file with out truncating the file to zero len

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
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

prepend a text file with out truncating the file to zero len

Post by dstefani »

What's the easiest way to pre-pend a file with fopen (or another way) so that the newest data is put at the beginning of the file?

Thaks,

D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

the only way is to rewrite the entire file, I believe.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

Code: Select all

$curData = file_get_contents($FILE);
	file_put_contents($FILE, $newData . $curData);
You will need to change $FILE to your file of course and if you want any lines between the new data and old data you will need to add that as well but I believe this is what you are looking for?


note this is PHP 5 dependent (file_put_contents()) but you can use fopen(), fwrite(), and fclose() to mimic this function in PHP < version 5
Post Reply