开发者社区> 问答> 正文

函数里引文件路径相对于谁??报错

文件目录结构

最外层的t.php

<?php
include './aa/function.php';
a();

报错.....


然后t.php要引入的函数文件 function.php
<?php
function a(){
 $a=include './bb/data.php';
  print_r($a);
}



最后 函数里引入的数据文件  data.php
<?php
return array('a'=>'ajax');



函数a里引入文件路径到底相对于谁啊,

如果函数文件和要引入的data.php在同一目录下,直接include 'data.php' 就对

不论这个函数在那个文件夹离得其他文件调用都没错,但如果函数文件和函数里要引入的文件不在同一目录下,像我上面的,怎么弄,我不想使用绝对路径


展开
收起
爱吃鱼的程序员 2020-06-22 11:11:31 492 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    同一路径直接用,父路径是../文件夹/文件,子路径就是文件价名/文件

    A  
      -A1
       --AA1
        a
      -B1
    index

    在index调用a就是A/A1/AA1/a

    a中调用index就是../../../index

    不知你是否看懂


    调用函数的文件与定义函数时引入的文件的关系我的重点不是目录层级关系,而是在函数里引用文件!!!

    文件在哪解析,就相对哪。。。

    引用来自“Romen”的评论

    文件在哪解析,就相对哪。。。

    文档里说的很清楚了

    http://www.php.net/manual/en/function.include.php

    Filesareincludedbasedonthefilepathgivenor,ifnoneisgiven,theinclude_pathspecified.Ifthefileisn'tfoundintheinclude_path,includewillfinallycheckinthecallingscript'sowndirectoryandthecurrentworkingdirectorybeforefailing.Theincludeconstructwillemitawarningifitcannotfindafile;thisisdifferentbehaviorfromrequire,whichwillemitafatalerror.Ifapathisdefined—whetherabsolute(startingwithadriveletteror\onWindows,or/onUnix/Linuxsystems)orrelativetothecurrentdirectory(startingwith.or..)—theinclude_pathwillbeignoredaltogether.Forexample,ifafilenamebeginswith../,theparserwilllookintheparentdirectorytofindtherequestedfile.

    php引入只有两种可能,

    相对于当前访问的php文件路径,
    相对于当前php的文件路径,如include'data.php';就找同目录下的文件.

    include'./data.php'; 那就是找访问php的目录.

    之前一直以为啥都不加和加上'./'是一样的,哎!

    引用来自“Tuesday”的评论


    php引入只有两种可能,

    相对于当前访问的php文件路径,
    相对于当前php的文件路径,如include'data.php';就找同目录下的文件.

    include'./data.php'; 那就是找访问php的目录.


    2020-06-22 11:11:47
    赞同 展开评论 打赏
问答分类:
PHP
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载