1 absolute, relative, and indeterminate paths
Relative path
A relative path refers to a path that begins with a.
Copy Code code as follows:
./a/a.php (relative to current directory)
.. /common.inc.php (relative superior directory),
Absolute path
Absolute path is with/beginning or c:/under Windows A similar path to the beginning of a letter, where the full path can uniquely determine the final address of a file without any reference path. For example
Copy Code code as follows:
/apache/wwwroot/site/a/a.php
c:/wwwroot/site/a/a.php
Path not determined
usually not to. or/beginning, nor is the Windows down letter:/ The path to begin with, such as
Copy Code code as follows:
Beginning to think this is also a relative path, but in PHP's include/require inclusion 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 include paths are handled: First, remember a conclusion: if the include path is relative or absolute, it will not go to the INCLUDE_PATH environment variable defined in Include_path (php.ini), or use the Set_include_ in the program Path (...) setting) to find the file.
Test environment Description
Note: The following discussion and conclusion is based on the environment: Suppose a=http://www.xxx.com/app/test/a.php, again emphasizing that the following discussion is about direct access to a.
2. Relative path:
A relative path requires a reference directory to determine the final path of the file, and in the inclusion resolution, regardless of how many layers are nested, this reference directory is the directory where the program execution entry file resides.
Example 1
A to define require './b/b.php '; Then b=[site]/app/test/b/b.php
b to define require './c.php '; Then c=[site]/app/test/c.php is not [site]/app/test/b/c.php
Example 2
A to define 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 to define 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 to define the require '.. /b.php '; Then b=[site]/app/b.php
b to define require './c/c.php '; //Then c=[site]/app/test/c/c.php is not [site]/app/c/c.php
Example 5
A to define the require '.. /inc/b.php '; Then b=[site]/app/inc/b.php
b to 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 to define the require '.. /inc/b.php '; Then b=[site]/app/inc/b.php
b to define require './c.php '; Then c=[site]/app/test/c.php is not [site]/app/inc/c.php
3. Absolute path
Absolute path is relatively simple, 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 form of the directory, but note that __file__ is a magic constants, regardless of when it is equal to write the statement of the PHP file in the absolute path, so dirname (__ FILE__) also always points to the absolute path of the PHP file where the statement is written, and does not have anything to do with whether the file is contained in another file.
Example 1
A to define the require '.. /b.php '; Then b=[site]/app/b.php
b defines the Require dirname (__file__). /c.php '; Then b=[site]/app/c.php
Example 2
A to define the require '.. /inc/b.php '; Then b=[site]/app/inc/b.php
b defines the Require dirname (__file__). /c.php '; Then b=[site]/app/inc/c.php always with B in the same directory
Conclusion: Whether B is used by a or is directly accessed
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 the B file resides;
b if require dirname (__file__). ' /c/c.php '; Always refer to the c.php file in the C subdirectory of the directory where the B file resides;
4. Indeterminate path
First of all, in the include_path defined by the inclusion directory to stitching [indeterminate path], found that the existence of the file contains a successful exit, if not found, the execution of the require statement in the directory of PHP files to join [indeterminate path] composed of the full path to find the file, If the file exists, it contains a successful exit, otherwise the inclusion file does not exist and an error occurs. An indeterminate path is easier to confuse than a recommended use.
5. The solution
Because the reference directory in relative path is the directory where the execution entry file is located, the "indeterminate" path is also easier to confuse, so the best solution is to use absolute path; For example, the contents of b.php are as follows, no matter where require b.php are require c.php by reference to b.php path
$dir = DirName (__file__);
Require ($dir. '.. /c.php ');
or define a generic function import.php, set it to "automatically introduce files in advance" and configure them in php.ini as follows
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
Copy 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 files, regardless of how many levels of "reference directory" are current files