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

Homepage displays, but child pages display require_once error

我有一个主页正确显示的网站,但是当我单击子页面时出现以下错误:

警告:require_once(../php/DB.php) [function.require-一次]:无法打开流:[= 中没有这样的文件或目录28=].php 第 75

致命错误:require_once() [function.require]:打开失败需要 '../php/DB.php' (include_path='。 :/home/content/74/9419674/html/PEAR/PEAR') 在 /home/user/public_html/config.php 第 75 行

我在主页上收到了这个错误,但能够追踪到源头并更正 link。我假设它没有正确读取 link,但我的头撞墙的时间已经够长了。

配置文件:

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)执行的。为了避免这样的 "mistakes",加载绝对路径:

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

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

  1. http://www.php-fig.org/psr/psr-4/
  2. https://getcomposer.org/doc/01-basic-usage.md#autoloading