php - 无法在PHP中获得正确的包含路径

标签 php include compiler-errors require

我阅读了其他一些相关问题,并认为这是最佳答案:

set_include_path(get_include_path().PATH_SEPARATOR."/path/to/program/root");

但是,我无法将其保存到正确的目录中,并且错误消息不够有用,因此我迷路了。我的用户区目录(共享主机)如下所示:
/home/linweb09/b/example.com-1050560306/user    # <-- this is my root

我试图重新组织程序,以便所有模型文件都位于此目录中:
{root}/program_name/library/

Web根文件夹是这样的:
{root}/htdocs/

因此,我将所有包含项更改为如下形式:
set_include_path(get_include_path().PATH_SEPARATOR
                 ."/home/linweb09/b/example.com-1050360506/user");
require_once "/program_name/library/some_module.php";

索引必须加载此包含才能工作,以验证用户身份:
/htdocs/admin/authenticate.php
# index.php
set_include_path(get_include_path().PATH_SEPARATOR
                 ."/home/linweb09/b/example.com-1050360506/user");
require_once "/htdocs/admin/authenticate.php";

我遇到了以下错误:

[warn] mod_fcgid: stderr: PHP Fatal error:
require_once() [function.require]: Failed opening required '/htdocs/admin/authenticate.php'
(include_path='.:/usr/share/pear:/usr/share/php:/home/linweb09/b/example.com-1050360506/user')
in /home/linweb09/b/example.com-1050360506/user/htdocs/admin/index.php on line 3

[warn] mod_fcgid: stderr: PHP Warning:
require_once(/htdocs/admin/authenticate.php) [function.require-once]: failed to open stream:
No such file or directory in
/home/linweb09/b/example.com-1050360506/user/htdocs/admin/index.php on line 3



但这是在正确的位置,并且错误提示没有此类文件或目录,因此我不确定如何配置此文件或目录。

我也尝试了这些,但得到了相同的错误:
set_include_path(get_include_path().PATH_SEPARATOR."/user");
set_include_path(get_include_path().PATH_SEPARATOR."/");

最佳答案

尝试删除前导斜线。 /htdocs/admin/authenticate.php表示您正在从根/进行工作。如果要搜索包含路径,则需要使用相对路径:

require_once "program_name/library/some_module.php";
require_once "htdocs/admin/authenticate.php";

顺便说一句,您只需要为每个脚本设置一次include路径-您不需要为每个include设置set_include_path()行,只需在脚本顶部设置一次即可。

关于php - 无法在PHP中获得正确的包含路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10383933/

相关文章:

php - 通过插入分隔线防止文本溢出

php - 一个文件夹中有 100 万个或更多文件,用于包含(缓存)

python 使用包含模式复制文件

c++ - 如何判断头文件包含在哪里?

input - Verilog 输入错误

javascript - 农业网格|仅获取完全更改的单元格数据

PHP mySQL 查询无法使用 SQL TIME 类型字段中的变量

php - Laravel [ErrorException] 反序列化(): Error at offset 78 of 82 bytes

c - Kotlin/Native - Windows 上的 cinterop

c - 无法使用 time_t 检查 C 中的最后修改时间