【问题标题】:Can't get the right include path in PHP无法在 PHP 中获得正确的包含路径
【发布时间】:2012-04-30 12:44:34
【问题描述】:

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

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 文件夹是这样的:

{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";

我遇到了这些错误:

[警告] mod_fcgid:stderr:PHP 致命错误:
require_once() [function.require]:无法打开所需的“/htdocs/admin/authenticate.php”
(include_path='.:/usr/share/pear:/usr/share/php:/home/linweb09/b/example.com-1050360506/user')
在 /home/linweb09/b/example.com-1050360506/user/htdocs/admin/index.php 第 3 行

[警告] mod_fcgid:标准错误:PHP 警告:
require_once(/htdocs/admin/authenticate.php) [function.require-once]:无法打开流:

中没有这样的文件或目录 /home/linweb09/b/example.com-1050360506/user/htdocs/admin/index.php 在第 3 行

但它位于正确的位置,并且错误提示没有这样的文件或目录,所以我不确定我应该如何配置它。

我也试过这些,我得到了同样的错误:

set_include_path(get_include_path().PATH_SEPARATOR."/user");
set_include_path(get_include_path().PATH_SEPARATOR."/");

【问题讨论】:

    标签: php include compiler-errors require


    【解决方案1】:

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

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

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

    【讨论】:

    • 我尝试了/user/(full-path) 作为 set_include_path 并且我尝试了htdocs/admin/authenticate.php 为每一个,仍然没有运气:(
    • 其实你完全正确!我刚刚再次检查了日志文件,但它在我需要更新的其他包含上失败了,感谢您的帮助:)。 (对于我之后的其他人,它使用完整路径,即/home/linweb09/.../user
    猜你喜欢
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2015-12-16
    • 2013-07-14
    • 1970-01-01
    • 2017-12-04
    相关资源
    最近更新 更多