dqq22391 2011-01-29 10:39
浏览 97
已采纳

路径混乱:相对/绝对/相对于DOCUMENT_ROOT?

I have just started learning PHP and I have read several answers to pretty much similar question, but it didn't help.

I have several files:

index.php 
folder1\file.php
folder2\file.php 

So index.php includes folder1\file.php and it is really easy. Then folder1\file.php includes folder2\file.php. The book I am reading says to do this I have to write this inside folder1\file.php:

require $_SERVER['DOCUMENT_ROOT'] . '/' . 'folder2/file.php';

So basically why didn't they do this?

require 'folder2/file.php';

As far as I understood, all the paths are always relative to the initial script's location and it works just fine on my machine.

I still don't get why not use .\folder\file.php which would keep it working if I move my directory deeper or copy the whole website to another machine.

According to the manual: If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory.

  • 写回答

2条回答 默认 最新

  • dongxiane0395 2011-01-29 11:06
    关注

    The PHP manual says:

    Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing.

    So it's always a best practice to set the include path in your master file (in your case, index.php) using the set_include_path() method.

    When working with frameworks or customised servers, it is good to specify your include path without leaving behind the system defined include paths by doing the following:

    set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
    

    That way, your new include path will be added to the existing paths.

    For what you have asked in the question, it is always better to specify the include path as I mentioned above and then refer your files relatively. If in case in future you want to move your files, editing just one line will do instead of finding and replacing in hundreds of files.

    Example:

    index.php:

    set_include_path(get_include_path() . PATH_SEPARATOR . $_SERVER['DOCUMENT_ROOT']);
    require('folder1/file.php');
    

    folder1/file.php:

    require('folder2/file.php');
    

    folder2/file.php:

    require('folder3/file.php');
    

    and so on..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?