addcontents.php
<?php
// your new data + newline
$new_line = 'some new data here'."\n";
// the filepath
$file = 'temp/file.txt';
// the old data as array
$old_lines = file($file);
// add new line to beginning of array
array_unshift($old_lines,$new_line);
// make string out of array
$new_content = join('',$old_lines);
$fp = fopen($file,'w');
// write string to file
$write = fwrite($fp, $new_content);
fclose($fp);
?>
extract from http://fundisom.com/phpsnippets/snip/file_handling/write_to_first_line_of_file/