• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

【解惑】PHP中include文件包含路径搜索问题

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 

首先我们来看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';

?>

不同的文件包含方式,程序的执行性能比较具体可以参照此文

PHP include文件性能研究


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
nginx默认访问index.php发布时间:2022-07-10
下一篇:
PHPfwrite()函数与file_put_contents()函数的比较发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap