• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

PHP include文件包含路径搜索问题的总结

php 搞代码 3年前 (2022-01-22) 12次浏览 已收录 0个评论

首先我们来看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  , you included  and there is include "b.php"  in that file,   is first looked in   and then in  . If filename begins with ./  or ../ , it is looked only in the current working directory.

寻找包含文件的顺序先是在当前工作目录的相对的 include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 . ,当前工作目录是 ,脚本中要 include 一个 并且在该文件中有一句 include "b.php" ,则寻找 的顺序先是 ,然后是 。如果文件名以 ./ 或者 ../ 开始,则只在当前工作目录相对的 include_path 下寻找。

所以如下所示的文件结构

—-a.php

—-i@本文来源gaodai$ma#com搞$代*码6网搞代gaodaima码nclude/b.php

—-include/c.php

其中a.php

<?phpinclude 'include/b.php';?>-----------------------b.php<?phpinclude 'c.php';include 'include/c.php';?>

————————–

c.php

<?phpecho 'c.php';?>

————————–

都能正确运行,说明b.php中两种不同包含路径都是可行的,根据include寻找包含文件的方式都能找到c.php。

但是最好的方式还是使用绝对路径,如果使用了绝对路径,php内核就直接通过路径去载入文件而不用去include path逐个搜索文件,增加了代码执行效率

<?phpdefine('ROOT_PATH',dirname(FILE));include ROOT_PATH.'/c.php';?>

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

<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src=" </script>

以上就是PHP include文件包含路径搜索问题的总结的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:PHP include文件包含路径搜索问题的总结
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址