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 8363777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:16:05+00:00 2026-06-09T12:16:05+00:00

I’ve spent days researching the internet and I can’t find the answer that I

  • 0

I’ve spent days researching the internet and I can’t find the answer that I will understand and are able to implement.

I’ve got the website in which I want to use include_once files and be able to view the website properly in both localhost and the actual server.

I’ve used $_SERVER['DOCUMENT_ROOT'] before but it doesn’t work on the localhost so it is hard for me to update and do the changes to the website.

What I want to do is to create some sort of config.php file in which I can tell it that the root folder is either called htdocs or public_html and to “start” looking for files from there.

Or is there any other way I can get include paths to work?

I’ve seen replies with e.g. dirname(__FILE__), __DIR__, etc. but they never explained how it would work, where to write it in the code, etc. I’m a bit sick and tired of finding part of the answer and then running around in circles just try and “fill in” the gaps. I’m not a PHP expert, just trying to make my life a bit easier by using it.

  • 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-09T12:16:06+00:00Added an answer on June 9, 2026 at 12:16 pm

    dirname(__FILE__) and __DIR__ both same and __DIR__ comes with PHP 5.3

    These are used in order to indicate that the “path of the file where they called”.

    URL: http://localhost/test/a.php
    
    DIR: --NIX
         /var/www/test/a.php
         --WIN
         D:\lamp\www\test\a.php
    
    // a.php's inside
    &lt?php
    echo __DIR__;
    

    Gives you this on linux: /var/www/test

    So, if you need a config parameter in all your project, just define it in your config.php and use it where you want both the file name that will be included.

    ./
      config.php
      index.php
      header.php
      footer.php
      /lib
        foo.php
      /tmp
        bar.php
    

    ./config.php
    define('ROOT', __DIR__ .'/');

    ./index.php include_once(ROOT .'header.php'); ... include_once(ROOT .'footer.php');

    i.e, using it in tmp dir

    ./tmp/bar.php include_once(ROOT .'lib/foo.php');

    UPDATE

    // config.php
    <?php
    define("ROOT", __DIR__ ."/");
    

    So, we use this for index.php to include banner.php and banner.php is waiting in ./banners/banner.php;

    // index.php and the very first line!
    <?php
    include_once("config.php");
    ?>
    // some html stuff
    // ...
    <?php include_once(ROOT ."banners/banner.php"); ?>
    // some more html stuff
    // ...
    

    So, you should include config.php first to where you need it.

    I think, this is basic as far as needed…

    UPDATE

    So your problem is not PHP include system, but question, anyway… 🙂

    If your image path is changing (so not fixed), you can do like this;

    // config.php
    define("ROOT", __DIR__ ."/");
    define("HTTP", ($_SERVER["SERVER_NAME"] == "localhost")
       ? "http://localhost/your_work_folder/"
       : "http://your_site_name.com/"
    );
    
    // banner.php
    <img src="<?php print HTTP; ?>images/banner.gif">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a small JavaScript validation script that validates inputs based on Regex. I

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.