views:

67

answers:

4

I use the following scheme to include PHP files:

require($_SERVER['DOCUMENT_ROOT'] . "Path/To/My/Website/Path/To/My/File.php");

I would like to save

$_SERVER['DOCUMENT_ROOT'] . 'Path/To/My/Website'

in some variable, say $my_website, and write:

require("$my_website/Path/To/My/File.php");

This way, if I decide to change the path to my website, I will need to modify it only in one place in my code.

Some PHP files may be included several times and from different directory levels. For example:

$_SERVER['DOCUMENT_ROOT']
                         Path
                             To
                               My
                                 Website
                                        Dir1
                                            a.php
                                        Dir2
                                            b.php that includes a.php
                                        Dir3
                                            Dir4
                                                c.php that includes a.php

However, I can't think how to do this.

Please suggest.

+1  A: 

I can think of two ways of doing this:

  1. Create a common file, included in every other file, that set your variable $my_website
  2. Add your website path to your include_path so you don't have to use you website path at all to include your files (require "Path/To/My/File.php";)
Keeper
In #1: how would you include that common file (what path would you use) ? Suppose in my example above that `a.php` is the common file.
Misha Moroshko
I'll put it in my website root and include it using relative path: this will work as long as you don't change your directory structure. So for your example b.php -> require '../Dir1/a.php' and c.php -> require '../../Dir1/a.php'. But as other answers noted it's best to use include_path
Keeper
+1  A: 

Use include path - will make your life simpler (you can control it even from .htaccess).

Emil Ivanov
A: 

I set constants for BASE_PATH and BASE_URI in my config.php file, which is in the same folder as the scripts and gets included in each script (require('config.php')):

define('BASE_PATH', '/filesystem/path/to/application');
define('BASE_URI', '/uri/home');

Then you can use:

require(BASE_PATH . '/file.php');

One further hint - if you have seperate development and live sites, you can put these definitions in a switch:

$mode = 'dev';
switch($mode){
    case 'dev':
        define('BASE_PATH', '/filesystem/path/to/application');
        define('BASE_URI', '/uri/home');
    break;
    case 'live':
        define('BASE_PATH', '/different/path/to/application');
        define('BASE_URI', '/');
    break;
}
adam
The question is what path do you use to include `config.php` ?
Misha Moroshko
Then maybe you should word it more clearly rather than downvoting. Either way, I put time and effort into providing an answer - how about saying "thanks, but..."?
adam
Sorry, man, but I didn't downvote you... I appreciate your effort to help.
Misha Moroshko
A: 

set_include_path is your friend:

<?php

set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT'] . '/path/to/my/website');

include 'Dir1/a.php';

http://uk2.php.net/manual/en/function.set-include-path.php

G Mawr
Should I do `set_include_path` in every file where I include `a.php` ?
Misha Moroshko
Personally, I would try and use the Front Controller pattern, if at all possible.
G Mawr