file handling
|write to first line of file
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.
full source of write to first line of file [ line 1 - 16 ] | download write to first line of file
| 1 | <?php |
| 2 | // your new data + newline |
| 3 | $new_line = 'some new data here'."n"; |
| 4 | // the filepath |
| 5 | $file = 'temp/file.txt'; |
| 6 | // the old data as array |
| 7 | $old_lines = file($file); |
| 8 | // add new line to beginning of array |
| 9 | array_unshift($old_lines,$new_line); |
| 10 | // make string out of array |
| 11 | $new_content = join('',$old_lines); |
| 12 | $fp = fopen($file,'w'); |
| 13 | // write string to file |
| 14 | $write = fwrite($fp, $new_content); |
| 15 | fclose($fp); |
| 16 | ?> |
35 hits by 16 users in the last 30 minutes.