This is simple, take everything from the text file, put it into a string, then write the new message, then "paste" the string in after it. Here's the code:
$fcontents = join ( '', file ( "messages.txt"));
$fp = fopen ("messages.txt", "w");
fwrite ($fp, "$message\n");
fwrite ($fp, $fcontents);
fclose ($fp);