doukong9316 2015-05-22 18:33
浏览 655
已采纳

PHP require_once绝对路径与相对路径(不工作)

Well, I am still very confused how this is possible. Look at the following two lines of code:

Code on Connection.php in class folder:

require_once '../config/constants.php';

require_once dirname(__DIR__).'/config/constants.php';

The second works totally fine, but the first throws the error of:

No such file in ...... on line... in Connection.php

It's getting on my nerves, what is the difference between the two? In my years of programming, I never encountered such problem? Am I missing some points?

If you have any documentation you would like to offer then provide. I would like to have a good foundation of knowledge than just solving problem.

Directory Structure:

enter image description here

  • 写回答

1条回答 默认 最新

  • duanji1899 2015-05-22 18:50
    关注

    ../ denotes the parent directory, compared to the one you're currently in. Therefore, your first line says "go one directory up and look for config/constants.php in there". What you'd want to use with a relative path here is ./ (note the single dot), which denotes your current directory.

    This has little to do with programming in specific, it's more of a file-system thing.

    You can use this as a reference:

    • leading / (no dots, just a slash) means an absolute path
    • leading ./ (single dot) means the current directory
    • leading ../ (two dots) means one directory up
    • no leading dots or slashes is the same thing as with the single dot - relative to the current directory.

    Update (to account for edited question):

    What has something to do with programming and PHP in particular is that what is considered the "current" directory is usually the one where you initially executed the script from. Under a CLI environment, that may vary, but if you're accessing the script via the web that is the location of the PHP file which you have directly accessed. E.g. if you're pointing your browser to example.com/test.php, then wherever 'test.php' is located is the directory you're currently in.

    That can be changed by chdir() of course.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?