El Anonimo
El Anonimo

Reputation: 1870

require_once failed to open stream: No such file or directory

The required library class seems to elude two .php files though it is found for the other one. The class (libraries/Database.php) methods are used in footer.php.

The error I receive is this:

Warning: require_once(libraries/Database.php): failed to open stream: No such file or directory in D:\PHP\Projects in PHP and MySQL\talkingspace\core\init.php on line 15

Fatal error: require_once(): Failed opening required 'libraries/Database.php' (include_path='C:\xampp\php\PEAR') in D:\PHP\Projects in PHP and MySQL\talkingspace\core\init.php on line 15

init.php:

<?php
// start session since we use it on every page
session_start();

// config file
require_once('config/config.php');

// helper functions
require_once('helpers/db_helper.php');
require_once('helpers/format_helper.php');
require_once('helpers/system_helper.php');

// for autoload of classes
function __autoload($class_name) {
    require_once('libraries/'.$class_name.'.php');
}

The index.php file displays correctly:

<?php require('core/init.php'); ?>
<?php
// create Topic object
$topic = new Topic();
// get Template class
$template = new Template('templates/frontpage.php');
// assign vars
$template -> topics = $topic -> getAllTopics();
$template -> totalTopics = $topic -> getTotalTopics();
$template -> totalCategories = $topic -> getTotalCategories();
// display template
echo $template;     

Its template file

<?php include 'includes/header.php'; ?>                         
<ul id="topics">
<?php if ($topics) : ?>
<?php foreach($topics as $topic) : ?>
    <li class="topic">
        <div class="row">
            <div class="col-md-2">
                <img class="avatar pull-left" src="images/avatars/<?php echo $topic -> avatar; ?>" />
            </div>
            <div class="col-md-10">
                <div class="topic-content pull-right">
                    <h3><a href="topic.html"><?php echo $topic -> title; ?></a></h3>
                    <div class="topic-info">
                        <a href="topics.php?category=<?php echo urlFormat($topic -> category_id); ?>"><?php echo $topic -> name; ?></a> >> <a href="topics.php?user=<?php echo urlFormat($topic -> user_id) ?>"><?php echo $topic -> username; ?></a> >> Posted on: <?php echo formatDate($topic -> create_date); ?> 
                        <span class="badge pull-right"><?php echo replyCount($topic -> id); ?></span>
                    </div>
                </div>
            </div>
        </div>
    </li>
<?php endforeach; ?>
</ul>
<?php else : ?>
    <p>No topics to display</p>
<?php endif; ?>
<h3>Forum Statistics</h3>
<ul>
    <li>Total Number of Users: <strong>52</strong></li>
    <li>Total Number of Topics: <strong><?php echo $totalTopics; ?></strong></li>
    <li>Total Number of Categories: <strong><?php echo $totalCategories; ?></strong></li>
</ul>
<?php include 'includes/footer.php'; ?>

serves well however a similar template file fails with two other .php files. Here's one (register.php):

<?php require('core/init.php'); ?>
<?php
// get Template class
$template = new Template('templates/register.php');
// assign vars
$template -> title = 'Create an Account';
// display template
echo $template;

and its template is:

<?php include 'includes/header.php'; ?>
    <form role="form" enctype="multipart/form-data" method="post" action="register.php">
        <div class="form-group">
            <label>Name<sup>*</sup></label>
            <input type="text" class="form-control" name="name" placeholder="Enter Your Name">
        </div>
        <div class="form-group">
            <label>Email Address<sup>*</sup></label>
            <input type="email" class="form-control" name="email" placeholder="Enter Your Email Address">
        </div>
        <div class="form-group">
            <label>Choose Username<sup>*</sup></label>
            <input type="text" class="form-control" name="username" placeholder="Create A Username">
        </div>
        <div class="form-group">
            <label>Password<sup>*</sup></label>
            <input type="password" class="form-control" name="password" placeholder="Enter A Password">
        </div>
        <div class="form-group">
            <label>Confirm Password<sup>*</sup></label>
            <input type="password" class="form-control" name="password2" placeholder="Enter Password Again">
        </div>
        <div class="form-group">
            <label>Upload Avatar</label>
            <input type="file" name="avatar">
            <p class="help-block"></p>
        </div>
        <div class="form-group">
            <label>About Me</label>
            <textarea id="about" rows="6" cols="80" class="form-control" name="about" placeholder="Tell us about yourself (Optional)"></textarea>
        </div>
        <input name="register" type="submit" class="btn btn-default" value="Register" />
    </form>
<?php include 'includes/footer.php'; ?>

Please assist. I tried a few solutions posted here to no avail. The XAMPP folder is the default one on C:\ and the project folder is on another drive.

Upvotes: 0

Views: 4938

Answers (1)

El Anonimo
El Anonimo

Reputation: 1870

The footer displays correctly on each page with a change in init.php:

function __autoload($class_name) {
    require_once('/PHP/Projects in PHP and MySQL/talkingspace/libraries/'.$class_name.'.php');
}

Inside httpd.conf the default DocumentRoot and Directory are changed to

DocumentRoot "D:/PHP"
<Directory "D:/PHP">

config.php defines BASE_URI as define("BASE_URI", "http://".$_SERVER['SERVER_NAME']."/Projects in PHP and MySQL/talkingspace/");

Upvotes: 2

Related Questions