Page 1 of 1
prepend a text file with out truncating the file to zero len
Posted: Wed May 02, 2007 6:18 pm
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
Posted: Wed May 02, 2007 6:30 pm
by John Cartwright
the only way is to rewrite the entire file, I believe.
Posted: Wed May 02, 2007 6:49 pm
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