显示主页,但子页面显示require_once错误


Homepage displays, but child pages display require_once error

我有一个网站,其中主页显示正确,但当我点击子页面我得到以下错误:

警告:require_once(../php/DB.php) [function. php]/home/user/public_html/config.php第75行

致命错误:require_once() [function。

:在/home/user/public_html/config.php中,第75行 : include_path='.:/home/content/74/9419674/html/PEAR/PEAR',打开失败要求'./php/DB.php' (include_path='.:/home/content/74/9419674/html/PEAR/PEAR')

我在主页上收到这个错误,但能够追踪到它的来源并纠正链接。我假设它没有正确阅读链接,但我已经把头撞在墙上够久了。

配置文件:

require_once '../php/DB.php';
$dsn = 'mysql://username:password@host/database';
$options = array(
    'debug'       => 2,
    'portability' => DB_PORTABILITY_ALL,
    );
$db =& DB::connect($dsn, $options);
$db->setOption('autofree', true);
$db->setOption('persistent', true);
$db->setFetchMode(DB_FETCHMODE_ASSOC);
if (PEAR::isError($db)) {
    echo 'Standard Message: ' . $db->getMessage() . "'n";
    echo 'Standard Code: ' . $db->getCode() . "'n";
    echo 'DBMS/User Message: ' . $db->getUserInfo() . "'n";
    echo 'DBMS/Debug Message: ' . $db->getDebugInfo() . "'n";
    die($db->getMessage());
}

子索引文件:

<?php
include "../config.php";
$titleText = "The Store";
$titleImage = "Store.T.gif";
include "../includes/header.php";
include "getcats.php";
$bannerImage = "Ban_Store.jpg";
include "../includes/top.php";
?>

我的文件结构是默认的GoDaddy cPanel结构。配置文件在public_html中,索引文件在子目录中。你能给予的任何帮助都会很了不起。提前感谢!

脚本不是从子页面的文件夹执行,而是(可能)从根文件夹(document-root,即public_html)执行。为了避免这样的"错误",使用绝对路径加载:

require __DIR__ . '/../config.php';

或者,查看PHP类自动加载。

  1. http://www.php-fig.org/psr/psr-4/
  2. https://getcomposer.org/doc/01-basic-usage.md半自动的