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.
After wholly agreeing with zerkms – PHP file modification is a Bad Idea – you do not need replacements to do this.
Just read the file in with file(), which places your PHP file in a convenient array. Then you may splice the array, or possibly:
Check file permissions after you have done this. They might have become more relaxed (or less. If you run this from command line as root recursively on a web tree, with most Apache installations you will find yourself with a unusable web site since Apache will no longer be able to access files that are now born-again superuser files).