【问题标题】:include _SERVER[ 'DOCUMENT_ROOT' ] . _SERVER[ 'SCRIPT_NAME' ] fails to find the currently running script包括_SERVER['DOCUMENT_ROOT']。 _SERVER[ 'SCRIPT_NAME' ] 找不到当前正在运行的脚本
【发布时间】:2013-04-06 16:09:25
【问题描述】:

我正在尝试使用 $_SERVER['DOCUMENT_ROOT'] 变量来包含文件。到目前为止,我尝试包含的每个文件都引发了 file-not-found 错误。我以为我只是弄错了目录,所以我尝试包含当前正在运行的脚本。我希望它递归地包含自己,直到它用完堆栈并摔倒。

echo( 'document root = ' . $_SERVER['DOCUMENT_ROOT'] . '<br>' );
echo( 'script name   = ' . $_SERVER['SCRIPT_NAME'] . '<br>' );

$szServerPath   = $_SERVER['DOCUMENT_ROOT'];
$szIncludePath  = $szServerPath . $_SERVER['SCRIPT_NAME'];
echo( "including     = " . $szIncludePath );
include( $szIncludePath );

这会产生以下输出:

document root = /var/httpd/htdocs
script name = /CSRC/Damflask/Main/Articles/index.php
including = /var/httpd/htdocs/CSRC/Damflask/Main/Articles/index.php
Warning: include_once(/var/httpd/htdocs/CSRC/Damflask/Main/Articles/index.php) [function.include]: failed to open stream: No such file or directory in /home/www/glmorriL/CSRC/Damflask/Main/Articles/index.php on line 33

它似乎仍然找不到该文件。我包含的所有其他文件都显示了相同的错误消息。为什么这不起作用?

编辑:似乎差异是由于“别名”。 http://php.net/manual/en/reserved.variables.server.php(参见 Jamie 两年前的评论)。 没有等效的路径吗?看起来 DOCUMENT_ROOT 对我来说完全没用。

谢谢, G

【问题讨论】:

    标签: php file include


    【解决方案1】:

    从报错信息可以看出你的文件路径应该是:

    /home/www/glmorriL/CSRC/Damflask/Main/Articles/index.php
    

    显然/var/httpd/htdocs/... 不会找到它。

    尝试改用__FILE__ 魔法常数。

    【讨论】:

    • 对不起,你能说得更具体点吗?我没用过__FILE__。 __FILE__ 会给我“/home/www/glmorriL”吗?
    • 在该文档中打印出__FILE__ 给了我:/home/www/glmorriL/CSRC/Damflask/Main/Articles/index.php 使用 DOCUMENT_ROOT 的意义在于它给了我根。我目前正在包含具有相对路径的文档。我想改用根路径。不幸的是,这对我没有帮助,因为它给了我当前文件的完整路径。
    • 显然__DIR__ 不存在,但我希望它会给我当前脚本正在运行的目录(而不是根目录)。
    猜你喜欢
    • 2013-08-12
    • 2011-12-13
    • 2010-10-21
    • 1970-01-01
    • 2011-01-07
    • 2011-02-13
    • 2011-09-22
    • 2014-06-18
    • 1970-01-01
    相关资源
    最近更新 更多