php nested include behavior

2019-01-15 04:36发布

In many places in my code, I do things like:

file1.php:
<?php
include('../file2.php');

file2.php:
<?php
include('anotherdirectory/file3.php');

Depending on the server or settings I try this on, it either sets the relative paths from the "includer" or from the "includee". This is really confusing. So file1 might try to include "../anotherdirectory/file3.php" or it might try "anotherdirectory/file3.php".

What settings dictate this behavior? I want to have control over this...

4条回答
贪生不怕死
2楼-- · 2019-01-15 04:50

In cases when I need to use relative paths I use the following syntax:

include (realpath(dirname(__FILE__)."/another_folder/myfile.php"));
查看更多
Animai°情兽
3楼-- · 2019-01-15 04:56

As someone on the php learning curve, I have found the best way to reference include paths is by absolute location, not relative, by using the built-in $_SERVER superglobal. In my own files I have been using this with success:

include $_SERVER [ 'DOCUMENT_ROOT' ] . '/path_from_root/file_name.php';

This way it doesn't matter where the included file resides relative to my calling file, and I don't have to worry about manually typing in my fully qualified server path. (Maybe obvious..) This will work no matter how nested the include call is, and if / when I move the calling file to a different directory, for example.

You can use this method with include, require, and any other file-related functions that need a path.

On a related note..

$_SERVER [ 'PHP_SELF' ]

will return the path (relative to the root) of the current file. I also use this quite a bit.

$_SERVER has other useful info you may want to check out here:

http://php.net/manual/en/reserved.variables.server.php

Sorry if this is an older thread, I'm new here.

EDIT: You could save this 'DOCUMENT_ROOT' to a variable for use later, but from recent experience I would recommend against it because then you have to worry about variable scope. The include line as written will work every time regardless of current scope.

查看更多
Melony?
4楼-- · 2019-01-15 04:59

I would recommend using absolute paths. A good way to do this while still being portable is to make a declaration like this in your public_html/index.php:

define('ROOT', dirname(__FILE__));

Then, you can write includes like this which are very easy:

include(ROOT.'/file.php');

Otherwise, PHP checks to see if the file is in the include path as defined by your php.ini. If it's not there, it tries a relative path to the current script. Which is unpredictable and unmaintainable since you may be nestingly including files from different relative locations.

Edit: If you're constantly including a lot of class files, you may want to look into autoloading. It makes everything way simpler if you're programming in an object-oriented style. I have personally never written the word 'include' in my code for a very long time.

Edit 2: You could use the php.ini directive auto_prepend_file to automatically include a one-line file with the definition of ROOT to each one of your scripts.

查看更多
祖国的老花朵
5楼-- · 2019-01-15 05:02

With get_include_path() you can see, what the server configuration for this is. In most cases it looks like this:

.:/usr/lib/php

This means, the first place php is looking for a included file is the directory of the script that includes another. If it is not present there, php is looking in /usr/php/lib. If you add more paths, php will also look there for a matching file.

If you include a file, which includes another one, the "root" path is the path of the file which included another one at first.

查看更多
登录 后发表回答