Summary of require and include path issues in PHP, requireinclude_php tutorial

Source: Internet
Author: User

Summary of 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.

./a/a.php (相对当前目录)    ../common.inc.php (相对上级目录),

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中定义  require './b/b.php';  // 则B=[SITE]/app/test/b/b.phpB中定义  require './c.php';    // 则C=[SITE]/app/test/c.php 不是[SITE]/app/test/b/c.php

Example 2

A中定义  require './b/b.php';  // 则B=[SITE]/app/test/b/b.php B中定义  require '../c.php';   // 则C=[SITE]/app/c.php  

Example 3

A中定义  require '../b.php';   //则B=[SITE]/app/b.php B中定义  require '../c.php';   //则C=[SITE]/app/c.php  

Example 4:

A中定义  require '../b.php';   // 则B=[SITE]/app/b.php B中定义  require './c/c.php';  / /则C=[SITE]/app/test/c/c.php  

Example 5

A中定义  require '../inc/b.php';  // 则B=[SITE]/app/inc/b.php B中定义  require './c/c.php';     // 则C还是=[SITE]/app/test/c/c.php  

Example 6

A中定义  require '../inc/b.php';  // 则B=[SITE]/app/inc/b.php B中定义  require './c.php';       // 则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';    // Linux中require 'c:/wwwroot/xxx.com/app/test/b.php';  // 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中定义  require '../b.php';                  // 则B=[SITE]/app/b.phpB中定义  require dirname(__FILE__).'/c.php';  // 则B=[SITE]/app/c.php

Example 2

A中定义  require '../inc/b.php';              // 则B=[SITE]/app/inc/b.phpB中定义  require dirname(__FILE__).'/c.php';  // 则B=[SITE]/app/inc/c.php 始终跟B在同一个目录

Conclusion: Whether B is used by a or is accessed directly

B如果 require dirname(__FILE__).'/c.php';    // 则始终引用到跟B在同一个目录中的 c.php文件; B如果 require dirname(__FILE__).'/../c.php'; // 则始终引用到B文件所在目录的父目录中的 c.php文件; B如果 require dirname(__FILE__).'/c/c.php';  // 则始终引用到B文件所在目录的c子目录中的 c.php文件;

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

更改配置项(必须)auto_prepend_file = "C:\xampp\htdocs\auto_prepend_file.php"更改配置项(可选)allow_url_include = On

import.php content is as follows

function import($path) {        $old_dir = getcwd();        // 保存原“参照目录”    chdir(dirname(__FILE__));    // 将“参照目录”更改为当前脚本的绝对路径    require_once($path);    chdir($old_dir);            // 改回原“参照目录”}

This allows you to use the import () function to require a file, regardless of how many levels of "reference directory" are current files

Reference article: PHP's require and include path issues experience Summary

http://www.bkjia.com/PHPjc/930707.html www.bkjia.com true http://www.bkjia.com/PHPjc/930707.html techarticle summary of require and include path issues in PHP, requireinclude 1 absolute path, relative path, and relative path relative path of an indeterminate path refers to a path that begins with a./a/a.php (relative when ...

  • 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.