【问题标题】:PHP - require_once: Failed to open stream: No such file or directoryPHP - require_once:无法打开流:没有这样的文件或目录
【发布时间】:2011-05-27 14:53:01
【问题描述】:

我似乎无法在 php 中打开此文件:

我的文件夹结构如下:

/home/www/site/user/index.php

/home/www/site/lib/login.inc.php

我正在尝试在用户 index.php 文件中打开 login.inc.php:

if (!defined('SITE_ROOT') || !defined('USER_ROOT') )
{
    define('SITE_ROOT', '../');
    define('USER_ROOT', './');
}

require_once SITE_ROOT . 'lib/login.inc.php';

但这不断出现无法打开流错误。

【问题讨论】:

标签: php require-once


【解决方案1】:

require_once 解析相对于工作目录的相对目录。当网络服务器执行 PHP 脚本以响应 HTTP 请求时,工作目录就是该 PHP 脚本的目录。当该 PHP 脚本包含其他 PHP 脚本时,其他 PHP 脚本仍将原始目录设置为工作目录。

这意味着包含文件相对于包含文件的位置无关紧要。为了弥补这一点,您可以在包含语句中使用绝对路径。在 /home/www/site/user/index.php 中,做

require_once dirname(__FILE__) . '/../lib/login.inc.php'

【讨论】:

    猜你喜欢
    • 2011-07-04
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 2015-02-25
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多