Hi,
you can try with file() function. This function reads the full file content in an array.
Now you can save the last line, add your new lines then add the last line you save. At the end just rewrite all to file.
Something like this:
$fcontents = file ($nameoffile);
$last=$fcontents[count($fcontents)-1];
$fcontents[count($fcontents)-1]=$outputstring;
$fcontents[]=$last;
$fp=fopen($nameoffile,"wb");
fwrite($fp,$fcontents);
fclose($fp);
I hope this helps...