Summary of require and include paths in PHP, requireinclude. In PHP, The require and include paths are summarized. the absolute path, relative path, and relative path of the undetermined paths refer. path starting. aa. php (compared with the require and include paths in PHP, requireinclude
1. absolute path, relative path, and unconfirmed relative path
The relative path refers to the path starting with ".", for example
./A. php (relative to the current directory) ../common. inc. php (relative to the upper 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
/apache/wwwroot/site/a/a.phpc:/wwwroot/site/a/a.php
Uncertain Path
Any path that does not start with a. or/or a drive letter in windows:/, for example
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 requiresReference DirectoryTo determine the final path of the file,No matter how many layers are nested, this reference directory is the directory where the program execution entry file is located..
Example 1
Require defined in '. /B. php '; // then B = [SITE]/app/test/B. define require in phpB '. /c. php '; // C = [SITE]/app/test/c. php is not [SITE]/app/test/B/c. php
Example 2
Require defined in '. /B. php '; // then B = [SITE]/app/test/B. define require in php B '.. /c. php '; // C = [SITE]/app/c. php is not [SITE]/app/test/c. php
Example 3
Define require in '.. /B. php '; // then B = [SITE]/app/B. define require in php B '.. /c. php '; // C = [SITE]/app/c. php is not [SITE]/c. php
Example 4:
Define require in '.. /B. php '; // then B = [SITE]/app/B. define require in php B '. /c. php '; // C = [SITE]/app/test/c. php is not [SITE]/app/c. php
Example 5
Define require in '.. /inc/B. php '; // then B = [SITE]/app/inc/B. define require in php B '. /c. php '; // C or = [SITE]/app/test/c. php is not [SITE]/app/inc/c. php
Example 6
Define require in '.. /inc/B. php '; // then B = [SITE]/app/inc/B. define require in php B '. /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'; // require 'C:/wwwroot/xxx.com/app/test/ B .php'in Linux; // in windows
Dirname (_ FILE _) is also an absolute path directory.__Is a Magic constants, no matter when it is equal to the absolute path of the php file where the statement is written, so dirname (_ FILE __) it always points to the absolute path of the PHP file that writes this statement, and has nothing to do with whether the file is included or used by other files.
Example 1
Define require in '.. /B. php '; // then B = [SITE]/app/B. in phpB, require dirname (_ FILE _) is defined __). '/c. php '; // then B = [SITE]/app/c. php
Example 2
Define require in '.. /inc/B. php '; // then B = [SITE]/app/inc/B. in phpB, require dirname (_ FILE _) is defined __). '/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 c. php FILE; B if require dirname (_ FILE __). '/.. /c. php '; // always reference c. php FILE; B if require dirname (_ FILE __). '/c. php '; // always reference c. php file;
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
BecauseThe "reference directory" in the "relative path" is the directory where the execution entry file is located., The "uncertain" 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:
Function import ($ path) {$ old_dir = getcwd (); // save the original "reference directory" chdir (dirname (_ FILE __)); // change "reference directory" to the absolute path require_once ($ path); chdir ($ old_dir) of the current script; // change it 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
Reference: PHP's experience on require and include paths
Relative paths of absolute paths, relative paths, and uncertain paths are relative paths starting with., for example,./a. php (relative...