Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8582991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:25:52+00:00 2026-06-11T21:25:52+00:00

I have a php file that I want to add an include path to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:25:53+00:00Added an answer on June 11, 2026 at 9:25 pm

    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:

    $file       = file('yourfile.php');
    
    $first_line = array_shift($file);       // Remove first line and save it
    array_unshift($file, $yourSecondLine);  // push second line
    array_unshift($file, $first_line);      // Save back the first line
    
    $fp = fopen('yourfile.php', 'w');       // Reopen the file
    fwrite($fp, implode("\n", $file));     
    fclose($fp);
    

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php file that returns a div and a javascript. I want
I have a file with .php extention www.example.com/thefile.php?name=123 that I want to direct the
I have a link which looks like that: www.domain.com/file.php I want to create .htaccess
I have a PHP file that I need it to detect it's directory it's
I have a php file that looks like this: <?php include(config.php); // put the
I have a PHP file that returns a JSON array. I need to extract
I want to set the PHP include path to another directory. I tried putting
I have a PHP file that contains a large array (about 90KB). I'm considering
I have a PHP file that contains a lot of if and else statements
I have a php file that contains a form (which contains 2 input boxes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.