If you're adding stuff onto the end of a text file, then when you fopen() the file, use "a" as the access modifier
$file=fopen($filename,"a");
Then when you write to $file, it will be appended to the end.
If you want to write to the start of the file, there's nothing for it but to read the whole file into memory, overwrite the file with the new material, then rewrite the old contents.