PHP的包含文件函数require和include路径总结
PHP的包含文件函数require和include路径总结
1 绝对路径、相对路径和未确定路径
相对路径
相对路径指以.开头的路径,例如
./a/a.php (相对当前目录)
../common.inc.php (相对上级目录),
绝对路径
绝对路径是以 / 开头或者windows下的 C:/ 类似的盘符开头的路径,全路径不用任何参考路径就可以唯一确定文件的最终地址。 例如
/apache/wwwroot/site/a/a.php
c:/wwwroot/site/a/a.php
未确定路径
凡是不以 . 或者 / 开头、也不是windows下 盘符:/ 开头的路径,例如
a/a.php
common.inc.php,
开始以为这也是相对路径,但在php的include/require包含机制中,这种类型的路径跟以 . 开头的相对路径处理是完全不同的。require './a.php' 和 require 'a.php' 是不同的!
下面分析这三种类型包含路径的处理方式:首先记住一个结论:如果包含路径为相对路径或者绝对径,则不会到include_path(php.ini 中定义的include_path环境变量,或者在程序中使用set_include_path(...)设置)中去查找该文件。
测试环境说明
注意:下面的讨论和结论基于这样的环境: 假设 A=http://www.xxx.com/app/test/a.php,再次强调下面的讨论是针对直接访问A的情况。
2. 相对路径:
相对路径需要一个参考目录才能确定文件的最终路径,在包含解析中,不管包含嵌套多少层,这个参考目录是程序执行入口文件所在目录。
示例1
A中定义 require './b/b.php'; // 则B=[SITE]/app/test/b/b.php
B中定义 require './c.php'; // 则C=[SITE]/app/test/c.php 不是[SITE]/app/test/b/c.php
示例2
A中定义 require './b/b.php'; // 则B=[SITE]/app/test/b/b.php
B中定义 require '../c.php'; // 则C=[SITE]/app/c.php 不是 [SITE]/app/test/c.php
示例3
A中定义 require '../b.php'; //则B=[SITE]/app/b.php
B中定义 require '../c.php'; //则C=[SITE]/app/c.php 不是 [SITE]/c.php
示例4:
A中定义 require '../b.php'; // 则B=[SITE]/app/b.php
B中定义 require './c/c.php'; / /则C=[SITE]/app/test/c/c.php 不是 [SITE]/app/c/c.php
示例5
A中定义 require '../inc/b.php'; // 则B=[SITE]/app/inc/b.php
B中定义 require './c/c.php'; // 则C还是=[SITE]/app/test/c/c.php 不是 [SITE]/app/inc/c/c.php
示例6
A中定义 require '../inc/b.php'; // 则B=[SITE]/app/inc/b.php
B中定义 require './c.php'; // 则C=[SITE]/app/test/c.php 不是 [SITE]/app/inc/c.php
3. 绝对路径
绝对路径的比较简单,不容易混淆出错,require|inclue 的就是对应磁盘中的文件。
require '/wwwroot/xxx.com/app/test/b.php'; // Linux中
require 'c:/wwwroot/xxx.com/app/test/b.php'; // windows中
dirname(__FILE__)计算出来的也是一个绝对路径形式的目录,但是要注意__FILE__是一个Magic constants,不管在什么时候都等于写这条语句的php文件所在的绝对路径,因此dirname(__FILE__)也总是指向写这条语句的php文件所在的绝对路径,跟这个文件是否被其他文件包含使用没有任何关系。
示例1
A中定义 require '../b.php'; // 则B=[SITE]/app/b.php
B中定义 require dirname(__FILE__).'/c.php'; // 则B=[SITE]/app/c.php
示例2
A中定义 require '../inc/b.php'; // 则B=[SITE]/app/inc/b.php
B中定义 require dirname(__FILE__).'/c.php'; // 则B=[SITE]/app/inc/c.php 始终跟B在同一个目录
结论:不管B是被A包含使用,还是直接被访问
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. 未确定路径
首先在逐一用include_path中定义的包含目录来拼接[未确定路径],找到存在的文件则包含成功退出,如果没有找到,则用执行 require语句的php文件所在目录来拼接[未确定路径]组成的全路径去查找该文件,如果文件存在则包含成功退出,否则表示包含文件不存在,出错。 未确定路径比较容易搞混不建议使用。
5. 解决方案
由于“相对路径”中的“参照目录”是执行入口文件所在目录,“未确定”路径也比较容易混淆,因此最好的解决方法是使用“绝对路径”; 例如b.php的内容如下,无论在哪里require b.php都是以b.php的路径为参照来require c.php的
$dir = dirname(__FILE__);
require($dir . '../c.php');
或者定义一个通用函数 import.php,将其设置为“自动提前引入文件”,在php.ini做如下配置
更改配置项(必须)auto_prepend_file = "C:xampphtdocsauto_prepend_file.php"
更改配置项(可选)allow_url_include = On
import.php内容如下
function import($path) {
$old_dir = getcwd(); // 保存原“参照目录”
chdir(dirname(__FILE__)); // 将“参照目录”更改为当前脚本的绝对路径
require_once($path);
chdir($old_dir); // 改回原“参照目录”
}
这样就可以使用import()函数来require文件了,无论包含多少级“参照目录”都是当前文件

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
