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.

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

报告相同问题?

悬赏问题

  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致
  • ¥15 QFILHelper怎么恢复全字库,提示进程已完成,只能恢复分区文件
  • ¥150 求 《小魔指》街机游戏机整合模拟软件
  • ¥20 你好,我想问下easyExcel下拉多选,或者复选框可以实现吗
  • ¥20 双非跨考工科哪个专业和方向就业前景好?
  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?