How to set root folder for PHP include files

73,061

Solution 1

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">

Solution 2

I normally set up what you mentioned, a config.php in an include directory, and have the following line in it: define('BASE_PATH', str_replace('/include', '', dirname(__FILE__))); ("/include should be whatever directory your config.php file is in, if you have it in the root directory, you can just use define('BASE_PATH', dirname(__FILE__));). When I want to include any other file after that, I use include_once(BASE_PATH . '/directory/file.php');.

note: this concept is not original to me by any means.

Solution 3

There is a function called getcwd() which will return the current working folder. If you call this at the very beginning of your script, you can store this, usually into a constant with define().

define('PROJECT_ROOT', getcwd());

Solution 4

To get/display the current work directory, php have a builtin function for that

 echo getcwd() . "\n";

This will display the current directory of your php file which is now being executed.

Share:
73,061
Vetaxili
Author by

Vetaxili

delete me

Updated on July 09, 2022

Comments

  • Vetaxili
    Vetaxili almost 2 years

    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.

  • Vetaxili
    Vetaxili almost 12 years
    I want to try and use this method but i don't seem to be able to get it right. do you think you could give me the example of how config.php would properly look like? What I've been trying shows errors and doesn't work. Also, if I have e.g. banner.php with images there what would be the correct path for them? /images/img01.jpg or images/img01.jpg :P
  • Vetaxili
    Vetaxili almost 12 years
    Includes work perfectly thank you :) I only have the problem with making images work on different directory levels.
  • Vetaxili
    Vetaxili almost 12 years
    Thank you ever so much Mister :-) I really appreciate your help
  • Andrew
    Andrew about 10 years
    All the many other stackoverflow answers on this subject should be deleted and replaced with this, thanks bud.
  • user46688
    user46688 about 8 years
    I looked everywhere online and couldn't find a better solution that this. Thank you so much @K-Gun !!!
  • Konstantinos Kamaropoulos
    Konstantinos Kamaropoulos about 7 years
    Sorry that I messed your perfect 6000 reputation but I had to upvote :P
  • vivanov
    vivanov almost 6 years
    if run from the console, getcwd() will return the current working directory of the console, not the directory of the php file, so if you run your script: php dir/index.php the returned path will be one level down.