1

I'm very new to PHP and I'm trying to use fwrite with this code:

<?php  
$File = "file.html"; //Load the file
$Handle = fopen($File, 'a');
$Data = $_GET["content"]; //Get the data
fwrite($Handle, $Data); // Write it
print "Data Written"; 
fclose($Handle);  
?>

It works perfectly. But the problem is I want to begin writing in the line 15, not in the end of the file. Is there a way to do this? Thank you!

MucaP
  • 992
  • 1
  • 10
  • 18
  • Text data are stored sequentially, you can only start a new file or append to an existing file. A solution may be to read and output up to line 14 and then continue writing your actual data. There are text-based databases which may support arbitrary access to records, but I think that is not what you are looking for. – syck Jul 26 '16 at 15:17
  • http://stackoverflow.com/questions/16813457/php-what-is-the-best-way-to-write-data-to-middle-of-file-without-rewriting-file – E_p Jul 26 '16 at 15:18
  • How? Full example if possible? – MucaP Jul 26 '16 at 15:19
  • Be more specific, then you may find someone to help you. This is not a code writing service. – syck Jul 26 '16 at 15:22
  • @syck What? What else can i say? You want to know my purpose? No, I don't think so... – MucaP Jul 26 '16 at 15:23
  • 1
    Read file per line into an array using `file` and then prepend your stuff to `$array[14]`. After you're done you `implode` the array back to a string with `PHP_EOL` (line break) and write to disk `file_put_contents`. – Charlotte Dunois Jul 26 '16 at 15:25
  • @mucap Your comment is not a technical question, it basically says "Can you write that code for me?" My answer would be: "Thats what I get paid for, make me an offer." =) – syck Jul 26 '16 at 15:30
  • @syck no, that's not, If you're not in a good mood or just don't wan't to help, that's ok. – MucaP Jul 26 '16 at 15:53

0 Answers0