instead of using file, it might be better to read into a string, insert the extra data to the front of the string, then write the string:
<?php
//define the file and data
$filename = "yourfile.txt";
$extra = "some data";
//open the file for reading
$fp = fopen($filename, "r")
OR die("Error: could not open file for reading");
$contents = fread($fp, filesize($filename));
fclose($fp);
//add the extra data to the front
//may need to insert a newline character in between
$contents = $extra . $contents;
//open the file for writing
$fp = fopen($filename, "w")
OR die("Error: could not open file for writing");
if (fwrite($fp, $contents))
echo 'Contents written';
else
echo 'Error: contents not written!';
fclose($fp);
?>
conversely, one might use only 1 fopen() call, but you might have to use file locking to be on the safe side.
Still...
<?php
//define the file and data
$filename = "yourfile.txt";
$extra = "some data";
//open the file for reading and writing
$fp = fopen($filename, "r+")
OR die("Error: could not open file for reading");
$contents = fread($fp, filesize($filename));
//add the extra data to the front
//may need to insert a newline character in between
$contents = $extra . $contents;
//reset the file pointer
rewind($fp);
//now write
if (fwrite($fp, $contents))
echo 'Contents written';
else
echo 'Error: contents not written!';
fclose($fp);
?>