Write to the second line of a PHP file

Posted by Woz on Stack Overflow See other posts from Stack Overflow or by Woz
Published on 2012-09-12T21:29:55Z Indexed on 2012/09/12 21:38 UTC
Read the original article Hit count: 194

Filed under:
|

I have a php file that I want to add an include path to on the second line. I need to open the file and inset a line of code on line 2.

I have tried a few techniques none of which are working but I think it has something to do with the text I am trying to write and possibly not escaping character correctly as I am not too familiar with file writing.

So here is the file I want to write to:

$file = $_SERVER['DOCUMENT_ROOT'].'/'.$domaindir.'/test.php';

Here is the piece of text I want to insert into the file:

$dbfile = "include('".$_SERVER['DOCUMENT_ROOT']."/".$domaindir."/web_".$dbname.".inc.php');";

Then what I was doing was a string replace but all it did was bump the "session_start();" bit to a newline!

Can anyone point me in the direction of a tutorial that might tell me how to insert this into the second line of my php file or indeed if anyone has any ideas?

I can say for sure that the path to the PHP file is fully tested so i know its not that the file is not being open or written to.

Any ideas would be much appreciated.

Thanks in advance.

© Stack Overflow or respective owner

Related posts about php

Related posts about file-io