Nested php include behavior

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'm trying to enable, it either sets the relative paths from the "includeer" or from the "includee". This is really confusing. Therefore, file1 may try to include "../anotherdirectory/file3.php" or it may try "anotherdirectory / file3.php".

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

+3
source share
4 answers

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

include (realpath(dirname(__FILE__)."/another_folder/myfile.php"));
+11
source

. , , public_html/index.php:

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

, :

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

PHP , include path, php.ini. , script. , .

: , . , - . "" .

2: php.ini auto_prepend_file, ROOT .

+8

- php, , - , , - $_SERVER. :

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

, , , , , . (, ..) , include, / , .

include, require , .

..

$_SERVER [ 'PHP_SELF' ]

( ) . .

$_ SERVER , :

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

, , .

EDIT: "DOCUMENT_ROOT" , , . , , .

+1

get_include_path() , . :

.:/usr/lib/php

This means that the first place php searches for the included file - this is the script directory, which includes another. If not, php looks in / usr / php / lib. If you add more paths, php will also look for the corresponding file there.

If you include a file that includes another, the “root” path is the path to the file that the other first included.

0
source

Source: https://habr.com/ru/post/1746050/


All Articles