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

  • Home
  • SEARCH
  • 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 628741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:40:10+00:00 2026-05-13T19:40:10+00:00

To keep URLs working in version-controlled projects, I’ve been using $_SERVER[‘DOCUMENT_ROOT’]. The problem is,

  • 0

To keep URLs working in version-controlled projects, I’ve been using $_SERVER[‘DOCUMENT_ROOT’]. The problem is, I develop projects within a folder, so I get this:

$_SERVER[‘DOCUMENT_ROOT’] . ‘/folder/path/to/file.php’

When I go live, I generally simply want the following:

$_SERVER[‘DOCUMENT_ROOT’] . ‘/path/to/file.php’

I know there are bigger problems in the world than having to remove and add this folder name, but is there a way I can easily automate this? Can I somehow set my document root locally to include the folder I’m working in? Do I have a fundamental misunderstanding of the way things are working? Kind of new at this stuff, and looking to learn as much as possible and really grok the “why.”

Thanks so much!

  • 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-05-13T19:40:10+00:00Added an answer on May 13, 2026 at 7:40 pm

    Instead of using $_SERVER['DOCUMENT_ROOT'], why not declare a constant which always holds the root of your web application?

    <?php
    define('ABSPATH', dirname(__FILE__));
    

    Put the following code in a file located in the root folder of your application and include it on every page load.

    Then, you can simply always do $path = ABSPATH . '/path/to/file.php'; regardless of if your local copy is in a sub-directory folder or not.


    If your application already has a file which is included on every page load, you can simply drop the code above in that file and it will work.

    Just note that you may have to add additional dirname() calls depending on where that file is located. Add one for each directory you pass from the root of your webapp.

    For example, if your webapp is located in /webapp/ and your “global include” is located in /webapp/includes/framework/init.php, then the above code needs to be modified as such:

    define('ABSPATH', dirname(dirname(dirname(__FILE__))));
    

    ie.: 2 additional dirname() calls due to two additional folders from the webapp root (includes/framework)


    Clarification

    The code above is meant to be in one file, and one file only in your web application. That file needs to be included on each page load.

    If you already have a file which is included before any processing (such as a configuration file or other), you may copy and paste that code in that file.

    The number of dirname() calls depends on how deep the file you copied and pasted the
    code in is relative to the root directory of your web application. For the examples above, assume the root of your web application is represented by ~.

    If you copy-paste my code into ~/abspath.php, then you need one dirname() call.

    If you copy-paste my code into ~/includes/abspath.php, then you need two dirname() calls.

    If you copy-paste my code into ~/includes/config/abspath.php, then you need three dirname() calls. Now let’s just say that’s its final location.

    In ~/index.php, you do the following:

    <?php
    require_once('includes/config/abspath.php');
    

    and you have access to ABSPATH.

    In ~/dir/someOtherPage.php you do the following:

    <?php
    require_once('../includes/config/abspath.php');
    

    and you have access to ABSPATH.

    This is why I’m saying that if you already have a file which is included on each page load, its simpler just to drop the above code in it. Just make sure you modify the amount of dirname() calls accordingly. Again, this code is meant to be in ONLY ONE FILE.

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

Sidebar

Ask A Question

Stats

  • Questions 357k
  • Answers 357k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The other answers are correct. Here is some code you… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer you ruin the noConflict concept by reassigning the jquery to… May 14, 2026 at 9:40 am
  • Editorial Team
    Editorial Team added an answer If you get that particular error, you don't actually have… May 14, 2026 at 9:40 am

Related Questions

No related questions found

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.