Detailed explanation of require and include paths in PHP, and the requireinclude_PHP tutorial

Source: Internet
Author: User
For details about the require and include paths in PHP, requireinclude. For details about the require and include paths in PHP, the absolute path, relative path, and relative path of the undetermined paths refer. for example, detailed descriptions of the require and include paths in the copy code PHP, and requireinclude

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

Relative paths of absolute paths, relative paths, and uncertain paths refer to paths starting with ".", for example, codes...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.