user2360599
user2360599

Reputation: 83

PHP include files from another directory that also include files

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.

Upvotes: 2

Views: 14724

Answers (4)

A Soft Vision
A Soft Vision

Reputation: 1

include "include/header.php";
include "include/head.php";

if you are select file from an other folder then coding will be like this

include "foldername/filename.php";

Upvotes: 0

alexander7567
alexander7567

Reputation: 664

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.

Upvotes: 1

Tivie
Tivie

Reputation: 18933

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');

Upvotes: 7

Anthony
Anthony

Reputation: 96

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')

Upvotes: 1

Related Questions