Search code examples
phpmysqlpear

Homepage displays, but child pages display require_once error


I have a website where the homepage displays correctly, but when I click on a child page I get the following error:

Warning: require_once(../php/DB.php) [function.require-once]: failed to open stream: No such file or directory in /home/user/public_html/config.php on line 75

Fatal error: require_once() [function.require]: Failed opening required '../php/DB.php' (include_path='.:/home/content/74/9419674/html/PEAR/PEAR') in /home/user/public_html/config.php on line 75

I was receiving this error on the home page but was able to trace it to the source and correct the links. I am assuming that it is not reading the link correctly, but I have beat my head against the wall long enough.

Config File:

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());
}

Child Index File:

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

My file structure is default GoDaddy cPanel structure. The config file is in public_html and the index file in a sub directory. Any help you can give would be amazing. Thanks in advance!


Solution

  • The script is not being executed from the folder of the sub-page, but (probably) from the root folder (document-root, i.e. public_html). To avoid "mistakes" like that, load with the absolute path:

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

    alternatively, look into PHP class autoloading.

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