Search code examples
phppathincluderequire

includes files not being found through require_once()


Hey guys before I begin my question, I'm gonna paste a photo of my folders structure so it will be easier to visualize.

enter image description here

Okay so, in the header.php highlighted above, I have this line of code:

<?php require_once("admin/admin_pages/includes/init.php"); ?>

And in that init.php file:

<?php
require_once("functions.php");
require_once("db_config.php");
require_once("database.php");
require_once("user.php");
require_once("session.php");
require_once("photo.php");
require_once("db_object.php");
?>

However, when I try to open the index.php file located in my root directory it gives this error:

enter image description here

The init.php that's being required_once is working. However it doesn't seem to find the files(which are in the same directory as the init.php) that are required in the init.php file itself. Why is this happening?

Note: All the paths work when I am playing around in my admin/admin_pages/index.php page.

enter image description here

EDIT: This is a screenshot of the contents in my includes folder.

<?php
//Just in case an includes file is not called in the init.php file.
function classAutoloader($class)
{
    $class = strtolower($class);
    $path = "includes/{$class}.php";

    if (file_exists($path)) {
        require_once($path);
    } else {
        die("The file named {$class}.php was not found.");
    }
}

spl_autoload_register('classAutoloader');

function redirect($location)
{
    header("Location: {$location}");
}
?>

EDIT: This is what is giving the error message FYI. I have setup an autoloader function. However, the file is not missing and should be loaded either way.


Solution

  • Hey guys thank you all for your input. I have finally found the problem with my code. It is not because of the autoloader function.

    I did not know that the order of the files being called in the init.php mattered. I have simply re-ordered the require_once() statements.

    The db_object.php file is actually the file that contains the parent class for my objects (photos and users). The photo.php and users.php contain inherited classes from the db_object.php. Which would made sense on why we need to find the db_object.php first.

    Before:

    <?php
    require_once("functions.php");
    require_once("db_config.php");
    require_once("database.php");
    require_once("user.php");
    require_once("session.php");
    require_once("photo.php");
    require_once("db_object.php");
    ?>
    

    After:

    <?php
    require_once("functions.php");
    require_once("db_config.php");
    require_once("database.php");
    require_once("db_object.php");
    require_once("user.php");
    require_once("session.php");
    require_once("photo.php");
    ?>