Log in

View Full Version : Resolved Put new data in front of existing data



M2com
07-27-2011, 05:06 AM
I know how to write to a txt file, but I want to put the new data to the beginning of the txt file (in front of existing data) not at the end. So the current updates will be at the top.

Thanks!

Beverleyh
07-27-2011, 12:35 PM
<?php
$lines = file('my_text_file.txt');
$fopen = fopen('my_text_file.txt', 'w+');
fwrite( $fopen, "A new line at the top.\r\n");
foreach ($lines as $line) { fwrite( $fopen, $line); }
fclose($fopen);
?>

M2com
07-27-2011, 04:37 PM
Thanks for the reply! Though when I used your script, it gave me this error: fwrite(): 2 is not a valid stream resource...

Thanks again!

M2com
07-27-2011, 04:40 PM
Never mind! Fixed it!

Thanks!