- 1 absolute path, relative path, and indeterminate path
- 2. Relative path:
- 3. Absolute path
- 4. Path not determined
- 5. Solution
1 absolute path, relative path, and indeterminate path relative path
A relative path refers to a path that begins with a.
./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
/apache/wwwroot/site/a/a.phpc:/wwwroot/site/a/a.php
Path not determined
usually not to. or/start, nor windows down letter:/ The beginning of the path, for example
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: assuming a=http://www.xxx.com/app/test/a.php, again 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 '; require './c.php ' is defined in B=[SITE]/APP/TEST/B/B.PHPB; Then c=[site]/app/test/c.php is not [site]/app/test/b/c.php
Example 2
A defines require './b/b.php '; Require ' is defined in b=[site]/app/test/b/b.php b . /c.php '; Then c=[site]/app/c.php
Example 3
A defines the require '. /b.php '; Require ' is defined in b=[site]/app/b.php b . /c.php '; Then c=[site]/app/c.php
Example 4:
A defines the require '. /b.php '; require './c/c.php ' is defined in b=[site]/app/b.php B; //Then c=[site]/app/test/c/c.php
Example 5
A defines the require '. /inc/b.php '; require './c/c.php ' is defined in b=[site]/app/inc/b.php B; Then C or =[site]/app/test/c/c.php
Example 6
A defines the require '. /inc/b.php '; require './c.php ' is defined in b=[site]/app/inc/b.php B; Then c=[site]/app/test/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 '; Require ' c:/wwwroot/xxx.com/app/test/b.php ' in Linux; 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 the absolute path of the PHP file where the statement is written, 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 '; require dirname (__file__) is defined in B=[SITE]/APP/B.PHPB. /c.php '; Then b=[site]/app/c.php
Example 2
A defines the require '. /inc/b.php '; require dirname (__file__) is defined in B=[SITE]/APP/INC/B.PHPB. /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, wherever require B.php are require c.php with the b.php path as a reference .
$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 (must) Auto_prepend_file = "C:\xampp\htdocs\auto_prepend_file.php" Change configuration item (optional) Allow_url_include = On
import.php content is 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
The directory where the current file is located is referenced by:
<?php include (' test.php ');? > or: <?php include ('./test.php ');? >
上级目录引用方法:
<?php
include
(
‘../test.php‘
);
?>
上上级引用方法:
<?php
include
(
‘../../test.php‘
);
?>
Reprint: http://www.cnblogs.com/rainman/p/4177302.html
Reference article: PHP's require and include path issues experience Summary
Summary of require and include path issues in PHP