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

报告相同问题?

悬赏问题

  • ¥15 ADS生成的微带线为什么是蓝色空心的
  • ¥15 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭