Require and include path issues in PHP, Requireinclude
1 absolute path, relative path, and indeterminate path
Relative path
A relative path refers to a path that begins with a.
Copy the Code code as follows:
./a/a.php (relative to current directory)
.. /common.inc.php (relative parent directory),
Absolute path
The absolute path is either the/start or the c:/under Windows Similar to the beginning of the drive letter path, the full path without any reference path can uniquely determine the final address of the file. For example
Copy the Code code as follows:
/apache/wwwroot/site/a/a.php
c:/wwwroot/site/a/a.php
Path not determined
usually not to. or/start, nor windows down letter:/ The beginning of the path, for example
Copy the Code code as follows:
a/a.php
common.inc.php,
Start thinking this is also a relative path, but in PHP's include/require containment mechanism, this type of path is followed by. The relative path processing at the beginning is completely different. Require './a.php ' and require ' a.php ' are different!
The following is an analysis of how these three types contain paths: First, remember one conclusion: if the containing path is a relative path or absolute diameter, it will not go to include_path (the INCLUDE_PATH environment variable defined in php.ini, or use Set_include_ in the program). Path (...) Settings) to find the file.
Test environment Description
Note: The following discussion and conclusions are based on the environment: Suppose a=http://www.xxx.com/app/test/a.php, again emphasizing that the following discussion is directed to the case of direct access a.
2. Relative path:
The relative path requires a reference directory to determine the final path of the file, and in the included parsing, regardless of how many layers are nested, this reference directory is the directory where the program executes the portal file.
Example 1
A defines require './b/b.php '; Then b=[site]/app/test/b/b.php
b define 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 in the definition of require '. /c.php '; Then c=[site]/app/c.php is not [site]/app/test/c.php
Example 3
A defines the require '. /b.php '; Then b=[site]/app/b.php
b in the definition of require '. /c.php '; Then c=[site]/app/c.php is not [site]/c.php
Example 4:
A defines the require '. /b.php '; Then b=[site]/app/b.php
b define require './c/c.php '; //Then c=[site]/app/test/c/c.php is not [site]/app/c/c.php
Example 5
A defines the require '. /inc/b.php '; Then b=[site]/app/inc/b.php
b define require './c/c.php '; Then C or =[site]/app/test/c/c.php is not [site]/app/inc/c/c.php
Example 6
A defines the require '. /inc/b.php '; Then b=[site]/app/inc/b.php
b define 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, it is not easy to confuse errors, Require|inclue is the corresponding disk files.
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 in the form of a directory, but note that __file__ is a magic constants, regardless of when it is equal to write this statement of the PHP file is located in the absolute path, so dirname (__ FILE__) also always points to the absolute path of the PHP file where the statement is written, with no relation to whether the file is being used by other files.
Example 1
A defines the require '. /b.php '; Then b=[site]/app/b.php
Require dirname (__FILE__) is defined in B. /c.php '; Then b=[site]/app/c.php
Example 2
A defines the require '. /inc/b.php '; Then b=[site]/app/inc/b.php
Require dirname (__FILE__) is defined 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 is accessed directly
b if require dirname (__file__). ' /c.php '; Always refer to the c.php file in the same directory as B;
b if require dirname (__file__). ' /.. /c.php '; Always refer to the c.php file in the parent directory of the directory where B files are located;
b if require dirname (__file__). ' /c/c.php '; Always refer to the c.php file in the C subdirectory of the directory where B files are located;
4. Path not determined
First, one by one using the include_path defined in the inclusion directory to stitch [the path], found the existence of the file contains a successful exit, if not found, then the execution of the require statement of the PHP file in the same directory to join the [indeterminate path] composed of the full path to find the file, If the file exists, it contains a successful exit, otherwise it indicates that the include file does not exist and an error occurred. It is not recommended to use a path that is not determined to be confusing.
5. Solution
Because the reference directory in relative path is the directory where the portal file is executed, the "indeterminate" path is also more confusing, so the best solution is to use an absolute path; For example, the contents of b.php are as follows, regardless of where require b.php is b.php's path as a reference 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 it as follows PHP.ini
Change configuration item (required) Auto_prepend_file = "C:\xampp\htdocs\auto_prepend_file.php"
Change configuration item (optional) Allow_url_include = On
import.php content is as follows
Copy the Code code as follows:
Function Import ($path) {
$old _dir = GETCWD (); Save the original "reference directory"
ChDir (DirName (__file__)); Change the reference directory to the absolute path of the current script
Require_once ($path);
ChDir ($old _dir); Change back to the original "reference directory"
}
This allows you to use the import () function to require a file, regardless of how many levels of "reference directory" are current files
http://www.bkjia.com/PHPjc/932469.html www.bkjia.com true http://www.bkjia.com/PHPjc/932469.html techarticle PHP require and include path issues, Requireinclude 1 absolute path, relative path and path relative path relative path refers to the path, such as copy code code ...