this snippet let's you insert data at the beginning of a file.
it reads the contents and adds new data to the first line of the file.
after this it joins the lines and writes them back to the file again.

write to first line of file” makes use of the built-in PHP functions file( ), array_unshift( ), join( ), fopen( ), fwrite( ) and fclose( ).

01 <?php
02 
03 // your new data + newline
04 $new_line = 'some new data here'."\n";
05  
06 // the filepath
07 $file = 'temp/file.txt';
08  
09 // the old data as array
10 $old_lines = file($file);
11  
12 // add new line to beginning of array
13 array_unshift($old_lines,$new_line);
14  
15 // make string out of array
16 $new_content = join('',$old_lines);
17 $fp = fopen($file,'w');
18  
19 // write string to file
20 $write = fwrite($fp, $new_content);
21 fclose($fp);
22  
23 ?>