0

Hello, I'm getting problem file or directory not found when I try to load the file from another directory. Kindly help me fix this problem. Thanks you.

Website
-classes
    -core.php
-inc
    -autoload.php
    -config.php
-index.php

Index.php

<?php
require_once('inc/autoload.php');
$core = new Core();
$core->run();

autoload.php

<?php
require_once('config.php');

function __autoload($class_name) {
    $class = explode("_", $class_name);
    $path = implode("/", $class).".php";
    require_once($path); <!-- echo $path shows 'core.php' -->
}

config.php

<?php
if(!isset($_SESSION)) {
    session_start();
}

// site domain name with http
defined("SITE_URL")
    || define("SITE_URL", "http://".$_SERVER['SERVER_NAME']);

// directory separator
defined("DS")
    || define("DS", DIRECTORY_SEPARATOR);

// root path
defined("ROOT_PATH")
    || define("ROOT_PATH", realpath(dirname(__FILE__) . DS."..".DS));

// classes folder
defined("CLASSES_DIR")
    || define("CLASSES_DIR", "classes");

// pages directory
defined("PAGES_DIR")
    || define("PAGES_DIR", "pages");

// modules folder
defined("MOD_DIR")
    || define("MOD_DIR", "mod");

// inc folder
defined("INC_DIR")
    || define("INC_DIR", "inc");

// templates folder
defined("TEMPLATE_DIR")
    || define("TEMPLATE_DIR", "template");

// emails path
defined("EMAILS_PATH")
    || define("EMAILS_PATH", ROOT_PATH.DS."emails");

// catalogue images path
defined("CATALOGUE_PATH")
    || define("CATALOGUE_PATH", ROOT_PATH.DS."media".DS."catalogue");

// add all above directories to the include path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(ROOT_PATH.DS.CLASSES_DIR),
    realpath(ROOT_PATH.DS.PAGES_DIR),
    realpath(ROOT_PATH.DS.MOD_DIR),
    realpath(ROOT_PATH.DS.INC_DIR),
    realpath(ROOT_PATH.DS.TEMPLATE_DIR),
    get_include_path()
)));

Core.php

<?php
class Core {

    public function run() {
        ob_start();
        require_once(Url::getPage());
        ob_get_flush();
    }

}

Thanks You.

Edited by chozotheqhai: add details

7
Contributors
7
Replies
79
Views
3 Years
Discussion Span
Last Post by diafol
0

You're probably getting something in the line of this:

Warning: require_once(Core.php): failed to open stream: No such file or directory in (yourpath)\inc\autoload.php on line 8

Fatal error: require_once(): Failed opening required 'Core.php' (include_path='./') in (yourpath)\inc\autoload.php on line 8

I am stuck with the same problem

0

You need to state the error you're getting.

0

You may be don't need this answer anymore, but I am answering.

I am having the same problem. So I tried again and again, then found the problem and solve. Everything is right in the code except

require_once('config.php');

Please replace that with

require_once('inc/config.php');

Now you are done. Because it wasn't getting the config file.

As the function by which you can call only the file name instead of additional path directory is defined in the config file. So, it will not work for it's own.

0

To autoload classes, the standard php library which is already built in php can do this in just three lines.

You have to place it in your core.inc.php or any php file that you will always include in your pages. And after this you will not touch the code anymore. It codes like:

spl_autoload_register (
    function ($class) {
        require_once 'classes/'.$class.'.php';
    }
);

That is all, you dont need to place any class name of anything into the function

But Note:
That the class name must correspond to the class created. Maybe the class file is stored as DB.php, the class in DB.php must be class DB {}

Edited by Gideon_1: spelling mistakes

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.