This article mainly introduces the detailed explanation of the require and include paths in PHP. For more information, see
This article mainly introduces the detailed explanation of the require and include paths in PHP. For more information, see
1. absolute path, relative path, and unconfirmed path
Relative Path
The relative path refers to the path starting with ".", for example
The Code is as follows:
./A. php (relative to the current directory)
../Common. inc. php (relative parent directory ),
Absolute path
The absolute path is the path starting with/or starting with a drive letter similar to C:/in windows. The full path can uniquely determine the final address of the file without any reference path. For example
The Code is as follows:
/Apache/wwwroot/site/a. php
C:/wwwroot/site/a. php
Uncertain Path
Any path that does not start with a. Or/or a drive letter in windows:/, for example
The Code is as follows:
A/a. php
Common. inc. php,
Initially, this is a relative path, but in the include/require inclusion mechanism of php, this type of path is completely different from the processing of the relative path starting. Require './a. php' and require 'a. php' are different!
The following describes how to deal with these three types of paths: first, remember a conclusion: If the paths contained are relative paths or absolute paths, they will not go to include_path (php. the include_path environment variable defined in ini, or use set_include_path (...) in the program (...) to find the file.
Test Environment Description
Note: The following discussion and conclusions are based on the following environment: Suppose A = http://www.xxx.com/app/test/a.php, And the next discussion is about the situation of direct tracing.
2. Relative Path:
The relative path requires a reference directory to determine the final path of the file. No matter how many layers are nested, this reference directory is the directory of the program execution entry file.
Example 1
Define require './B. php' in a; // then B = [SITE]/app/test/B. php
Define require in B '. /c. php '; // C = [SITE]/app/test/c. php is not [SITE]/app/test/B/c. php
Example 2
Define require './B. php' in a; // then B = [SITE]/app/test/B. php
B defines require '../c. php'; // then C = [SITE]/app/c. php is not [SITE]/app/test/c. php
Example 3
Define require '../B. php' in a; // then B = [SITE]/app/B. php
B defines require '../c. php'; // then C = [SITE]/app/c. php is not [SITE]/c. php
Example 4:
Define require '../B. php' in a; // then B = [SITE]/app/B. php
Define require in B '. /c. php '; // C = [SITE]/app/test/c. php is not [SITE]/app/c. php
Example 5
Define require '../inc/B. php' in a; // then B = [SITE]/app/inc/B. php
Define require in B '. /c. php '; // C or = [SITE]/app/test/c. php is not [SITE]/app/inc/c. php
Example 6
Define require '../inc/B. php' in a; // then B = [SITE]/app/inc/B. php
B defines require './c. php'; // C = [SITE]/app/test/c. php is not [SITE]/app/inc/c. php
3. absolute path
The absolute path is relatively simple and is not prone to confusion errors. require | inclue is the file in the corresponding 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 an absolute path directory, but note that _ FILE _ is a Magic constants, no matter when it is equal to the absolute path of the php file for writing this statement, dirname (_ FILE _) always points to the absolute path of the php file for writing this statement, it has nothing to do with whether the file is included or used by other files.
Example 1
Define require '../B. php' in a; // then B = [SITE]/app/B. php
Define require dirname (_ FILE _). '/c. php' in B; // then B = [SITE]/app/c. php
Example 2
Define require '../inc/B. php' in a; // then B = [SITE]/app/inc/B. php
Define require dirname (_ FILE _) in B __). '/c. php '; // then B = [SITE]/app/inc/c. php is always in the same directory as B.
Conclusion: Whether B is used by A or accessed directly
B. If require dirname (_ FILE _). '/c. php'; // always reference the c. php FILE in the same directory as B;
B. If require dirname (_ FILE _). '/../c. php'; //, always reference the c. php FILE in the parent directory of file B;
B. If require dirname (_ FILE _). '/c. php'; // always reference the c. php FILE in the c subdirectory of the directory where file B is located;
4. Uncertain Path
First, use the include directory defined in include_path to concatenate [uncertain path]. If an existing file is found, the file is included and exited successfully. If not, the directory where the PHP file that runs the require statement is located is used to concatenate a full path consisting of [uncertain paths] to find the file. If the file exists, the file is successfully exited. Otherwise, the file does not exist and an error occurs. Unconfirmed paths are easier to mix and not recommended.
5. Solution
Because the "reference directory" in the "relative path" is the directory where the execution entry file is located, the "unconfirmed" path is also easy to confuse, so the best solution is to use "absolute path "; for example, B. php content is as follows, no matter where require B. php is based on B. for php paths, refer to require c. php
$ Dir = dirname (_ FILE __);
Require ($ dir. '../c. php ');
Or define a general function import. php, set it to "automatically introduce files in advance", and configure the following in php. ini:
Change configuration item (required) auto_prepend_file = "C: \ xampp \ htdocs \ auto_prepend_file.php"
Change configuration item (optional) allow_url_include = On
The content of import. php is as follows:
The Code is as follows:
Function import ($ path ){
$ Old_dir = getcwd (); // Save the original "Reference Directory"
Chdir (dirname (_ FILE _); // change "Reference Directory" to the absolute path of the current script
Require_once ($ path );
Chdir ($ old_dir); // change back to the original "Reference Directory"
}
In this way, you can use the import () function to request the file. No matter how many levels of "Reference directories" are contained, the current file