【解惑】PHP中include文件包含路径搜索问题
2011-04-10 17:34
746 查看
首先我们来看php官方手册中对include的文件搜索原则的描述:
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries , current working directory is /www/ , you includedinclude/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/ . If filename begins with ./ or ../ , it is looked only in the current working directory.
寻找包含文件的顺序先是在当前工作目录的相对的 include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 . ,当前工作目录是 /www/ ,脚本中要 include 一个 include/a.php 并且在该文件中有一句 include "b.php" ,则寻找 b.php 的顺序先是 /www/ ,然后是 /www/include/ 。如果文件名以 ./ 或者 ../ 开始,则只在当前工作目录相对的 include_path 下寻找。
所以如下所示的文件结构
----a.php
----include/b.php
----include/c.php
其中a.php
<?php
include 'include/b.php';
?>
-----------------------
b.php
<?php
include 'c.php';
include 'include/c.php';
?>
--------------------------
c.php
<?php
echo 'c.php';
?>
--------------------------
都能正确运行,说明b.php中两种不同包含路径都是可行的,根据include寻找包含文件的方式都能找到c.php。
但是最好的方式还是使用绝对路径,如果使用了绝对路径,php内核就直接通过路径去载入文件而不用去include path逐个搜索文件,增加了代码执行效率
<?php
define('ROOT_PATH',dirname(__FILE__));
include ROOT_PATH.'/c.php';
?>
不同的文件包含方式,程序的执行性能比较具体可以参照此文
Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries , current working directory is /www/ , you includedinclude/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/ . If filename begins with ./ or ../ , it is looked only in the current working directory.
寻找包含文件的顺序先是在当前工作目录的相对的 include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 . ,当前工作目录是 /www/ ,脚本中要 include 一个 include/a.php 并且在该文件中有一句 include "b.php" ,则寻找 b.php 的顺序先是 /www/ ,然后是 /www/include/ 。如果文件名以 ./ 或者 ../ 开始,则只在当前工作目录相对的 include_path 下寻找。
所以如下所示的文件结构
----a.php
----include/b.php
----include/c.php
其中a.php
<?php
include 'include/b.php';
?>
-----------------------
b.php
<?php
include 'c.php';
include 'include/c.php';
?>
--------------------------
c.php
<?php
echo 'c.php';
?>
--------------------------
都能正确运行,说明b.php中两种不同包含路径都是可行的,根据include寻找包含文件的方式都能找到c.php。
但是最好的方式还是使用绝对路径,如果使用了绝对路径,php内核就直接通过路径去载入文件而不用去include path逐个搜索文件,增加了代码执行效率
<?php
define('ROOT_PATH',dirname(__FILE__));
include ROOT_PATH.'/c.php';
?>
不同的文件包含方式,程序的执行性能比较具体可以参照此文
PHP include文件性能研究
相关文章推荐
- 【解惑】PHP中include文件包含路径搜索问题
- PHP的包含(include,require)文件的路径问题
- PHP包含文件的路径问题
- php中include包含页面路径问题解决方法
- PHP 包含文件路径问题
- 谨防php包含文件的相对路径问题导致的crontab无法正常运行
- 【Php】include(require)嵌套包含文件时的一些注意问题
- php中使用include(require)嵌套包含文件时的一些注意问题
- php中include包含文件路径查找过程
- php中include,require的文件包含问题,以及$_SERVER['PHP_SELF']和__FILE__的区别
- PHP中文件包含的路径问题
- php url路径问题和php文件以绝对路径引入
- opencv 头文件 包含 include 的问题
- PHP中require和include路径问题总结
- PHP中require和include路径问题总结
- 整理Linux下gcc编译中关于头文件与库文件搜索路径相关问题
- PHP中require和include路径问题详解
- PHP require和include 路径问题
- gcc头文件库文件搜索路径问题(转…
- 菜鸟教你学编程之PHP(7)包含文件 include与require