一起源码网

  • www.171739.xyz
  • 全球最大的互联网技术和资源下载平台
搜索
猜你喜欢
查看: 6866|回复: 1
打印 上一主题 下一主题

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

[复制链接]

0

主题

0

帖子

1万

积分

钻石会员

Rank: 8Rank: 8

积分
17424
QQ
跳转到指定楼层
楼主
发表于 2020-3-4 17:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

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

----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';
?>

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

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

分享到:  QQ好友和群QQ好友和群
收藏收藏
回复

使用道具 举报

0

主题

13

帖子

41

积分

新手上路

Rank: 1

积分
41
沙发
发表于 2022-9-2 18:43 来自手机 | 只看该作者
百度文库免费下载入口
回复

使用道具 举报

一起源码让程序更轻更快

www.171739.xyz

工作时间 周一至周六 8:00-17:30

侵权处理

客服QQ点击咨询

关注抖音号

定期抽VIP

Copyright © 2016-2021 https://www.171739.xyz/ 滇ICP备13200218号

快速回复 返回顶部 返回列表