Home >Backend Development >PHP Tutorial >PHP: require and include path issues
FILE is a preprocessed variable, processed before running, and has been replaced before the file is included.
The content of the file required include is processed at runtime, and its code is run in the space of the included file, a relative path, relative to the included file.
1 Absolute path, relative path and undetermined path
Relative path
Relative path refers to the path starting with ., such as
./a/ a.php (relative to the current directory)
../common.inc.php (relative to the upper-level directory),
absolute path
The absolute path starts with / or windows C:/ similar to the path starting with the drive letter, the full path can uniquely determine the final address of the file without any reference path. For example
/apache/wwwroot/site/a/a.php
c:/wwwroot/site/a/a.php
Undetermined path
Any path that does not start with . or /, nor does it start with the Windows drive letter:/, such as
a/a.php
common.inc.php,
At first I thought this was also a relative path, but in PHP's include/require inclusion mechanism, this type of path is handled completely differently from relative paths starting with . require './a.php' and require 'a.php' are different!
Let’s analyze the processing methods of these three types of include paths: First, remember a conclusion: if the include path is a relative path or an absolute path, it will not go to include_path (the include_path environment variable defined in php.ini, Or use set_include_path(...) to find the file in the program.
Test environment description
Note: The following discussion and conclusion are based on this environment: Assume A=http://www.xxx.com/app/test/a.php, emphasize again The following discussion is for the case of direct access to A.
2. Relative path:
The relative path requires a reference directory to determine the final path of the file. In include parsing, no matter how many levels of nesting are included, this reference directory is the program execution Entry file directory.
Example 1
A defines require './b/b.php'; // then B=[SITE]/app/test/b/b.php
B Definition require './c.php'; // Then C=[SITE]/app/test/c.php is not [SITE]/app/test/b/c.php
Example 2
A defines require './b/b.php'; // Then B=[SITE]/app/test/b/b.php
B defines require '.. /c.php'; // Then C=[SITE]/app/c.php is not [SITE]/app/test/c.php
Example 3
A defines require '../b.php'; //then B=[SITE]/app/b.php
B defines require '../c.php'; //then C= [SITE]/app/c.php is not [SITE]/c.php
Example 4:
A defines require '../b.php' ; // Then B=[SITE]/app/b.php
Define require './c/c.php' in B; // Then C=[SITE]/app/test/c/c.php Not [SITE]/app/c/c.php
As defined in Example 5
A require '../inc/b.php'; // then B=[SITE]/app/inc/b.php
B requires require './c/c.php'; // Then C still =[SITE]/app/test/c/c.php No [SITE]/app/inc/c/c.php
Example 6
A defines require '../inc/b.php'; // Then B=[SITE]/app/inc/b.php
B requires require './c.php'; // Then C=[SITE]/app/test/c.php is not [SITE]/ app/inc/c.php
3. Absolute path
The absolute path is relatively simple and not easy to cause confusion and errors. require|inclue corresponds to the file on the disk. .
require '/wwwroot/xxx.com/app/test/b.php'; // In Linux
require 'c:/wwwroot/xxx.com/app/test/b.php' ; // In windows,
dirname(FILE) is also calculated as a directory in the form of an absolute path, but please note that FILE is a Magic constants, which is equal to The absolute path of the PHP file where this statement is written. Therefore, dirname(FILE) always points to the absolute path of the PHP file where this statement is written. It has nothing to do with whether this file is included and used by other files.
Example 1
A defines require '../b.php'; FILE).'/c.php'; // Then B=[SITE]/app/c.php
Define require '../inc/b.php'; in A php'; // Then B=[SITE]/app/inc/c.php is always in the same directory as B
Bif require dirname(FILE). '/../c.php'; // Always reference the c.php file in the parent directory of the directory where the B file is located;
BIf require dirname(FILE).'/c/c.php'; // Then always refer to the c.php file in the c subdirectory of the directory where the B file is located;
require($dir . '../c.php');
Change the configuration item (optional) allow_url_include = On
function import($path) { $old_dir = getcwd(); // 保存原“参照目录” chdir(dirname(FILE)); // 将“参照目录”更改为当前脚本的绝对路径 require_once($path); chdir($old_dir); // 改回原“参照目录” }In this way, you can use the import() function to require the file. No matter how many levels of "reference directories" it contains, it is the current file
The above is the detailed content of PHP: require and include path issues. For more information, please follow other related articles on the PHP Chinese website!