Php 5.4 Include / require -- 无法获取正确的文件路径

Php 5.4 Include / require -- cannot get file path right

假设我有这个文件:

/Home/user/docs/somewhere/inHere.php

并且在这个 php 中,我想要求这个:

/Home/user/other/well/buried/place.php

我知道绝对路径和相对路径之间的区别,但似乎无法弄清楚 php 希望它看起来如何,我不断得到 'file not found or does not exist'

我在一个 hostgator 共享网络服务器上,如果这对任何事情有影响的话。

从文件位置(某处)你必须向上(到文档)和另一次(到用户),然后进入 "other",像这样:

include ("../../other/well/buried/place.php");

给你。

使用绝对路径包含它。

或者:

include '/Home/user/other/well/buried/place.php'; 

或者相对于你所在的位置,但仍然是绝对的:

include __DIR__ . '/../../other/well/buried/place.php';

魔术常数 __DIR__ 包含写入文件的绝对路径。

如果您只执行相对路径 include '../../and-so-on',如果您将该文件包含在驻留在其他位置的其他文件中,则起点将会改变。

首先你可以使用getcwd()获取当前文件夹。

接下来可以使用$path = '../../etc'; $realPath = realpath($path)。如果路径错误,它将return false,如果确实是实际路径,则没有相对../的具体路径。

如果还是搞不定,var_dump($path);然后复制路径cd进去试试,到时候你就可以发现自己做错了什么了。