简体   繁体   中英

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.

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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM