PHP include files from another directory that also include files

13,359

Solution 1

The best way, to prevent changes in CWD that might break relative paths is to include files using absolute paths.

An easy way to accomplish this is by using the __DIR__ constant.

Example:

File structure:

serverRoot (/)
    |-usr
        |-local
            |-www
                |-index.php
                |-bootstrap.php
                |-includes
                    |-a.php
                    |-overall
                        |-b.php
                        |-c.php

let's say that:

  • index.php includes bootstrap.php and a.php
  • a.php includes b.php
  • bootstrap.php includes c.php

index.php

$basedir = realpath(__DIR__);
include($basedir . '/bootstrap.php');
include($basedir . '/includes/a.php');

a.php

global $basedir;
include($basedir . '/includes/overall/b.php');

bootstrap.php

global $basedir;
include($basedir . '/includes/overall/c.php');

Solution 2

You can check this part of the PHP documentation : http://www.php.net/manual/en/function.include.php

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include will finally check in the calling script's own directory and the current working directory before failing.

I suggest you either :

  • To include with absolute paths (not recommended, quick and dirty)
  • Change your include_path (php.ini) so you won't have any problems!
  • To set up an autoloading system : http://php.net/manual/en/language.oop5.autoload.php (but it's more for classes, may not help you but it's important to know it exists)
  • To use the _DIR_ constant (see Tivie reply) if you have one unique PHP file "entrance" (the "bootstrap file") which is called all the time (which should be the case!)

Also, when you say :

for example i have this include in my postpage.php which is located at example.com/c/postpage.php:

Be sure to understand fully the fact that the "path" URL (example.com/foo) and the path file on the disk can be totally different, and totally unlinked.

So, in your case, as you seems to have one directory for layout stuff (header/footer/...) and maybe one other for templates, the easiest thing is probably to add these 2 paths in your php.ini.

Then just call : include('head.php') and you're done.

You can also just add in your include_path the base directory of your project, and then call include('includes/overall/head.php')

Solution 3

I do this by creating a variable on the beginning of every page. The variable is simply the path to the top level directory.

example...

instead of

 include 'includes/head.php';

use:

 $pathToRoot = '../';
 include $pathToRoot.'includes/head.php';

but you will have to create this variable on the top of every page and change all include and require statements.

Share:
13,359
user2360599
Author by

user2360599

Updated on June 05, 2022

Comments

  • user2360599
    user2360599 over 1 year

    I have includes in multiple files, i recently started a new directory and am unable to get some of my includes to work.

    for example i have this include in my postpage.php which is located at example.com/c/postpage.php:

    include '../includes/overall/header.php';
    

    which works as it goes to the root directory and includes header.php but my problem is that header.php also includes files which are not being included.

    the code in header.php is:

    include 'includes/head.php';
    

    which is not working

    if i change header.php to:

    include '../includes/head.php';
    

    then it breaks the rest of my site, while working only for postpage.php

    any advice is welcome.

  • user2360599
    user2360599 over 10 years
    right but a majority of my pages dont want to send the include up a directory. i guess what im asking may not be possible
  • user2360599
    user2360599 over 10 years
    the $basedir will contain the same values in a.php and bootstrap.php even if its declared in a completely different file?
  • alexander7567
    alexander7567 over 10 years
    this is kindly like what I meant... just a better way of doing it. this should deffinantly fix it.
  • Tivie
    Tivie over 10 years
    Yes, its just like any other variable declared in the global scope. You declare it in the point of entry of your script (usually the index.php file) and use it throughout your script
  • alexander7567
    alexander7567 over 10 years
    I really like this method.. I may start using this on my sites.
  • user2360599
    user2360599 over 10 years
    ya this worked, just added in the variable globally. thank you very much
  • Tivie
    Tivie over 10 years
    glad to hear it. no prob mate