-4

Warning: require_once(pages/admin.php): failed to open stream: No such file or directory in C:\xampp\htdocs\bisnis\config\init.php on line 6

Fatal error: require_once(): Failed opening required 'pages/admin.php' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\bisnis\config\init.php on line 6

<?php
    session_start();

    //load class
    spl_autoload_register(function($class){
        **require_once 'pages/' .$class. '.php';**
    });
    $user = new user();
    $admin = new admin();


?>
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
feb
  • 5
  • 1
    Possible duplicate of [PHP - Failed to open stream : No such file or directory](https://stackoverflow.com/questions/36577020/php-failed-to-open-stream-no-such-file-or-directory) – Spoody Aug 22 '18 at 07:32
  • 3
    Like the error says. Your file `pages/admin.php` doesn't exist. It's probably looking in the wrong folder. Note that the require once is being called from your init.php script so it's currently looking for `C:\xampp\htdocs\bisnis\config\pages\admin.php`. It's probably not in that folder. You can start your `require_once` with a `/` to indicate that it should start looking from the root folder or use `../` to "go up" a folder. – Dirk Scholten Aug 22 '18 at 07:34

1 Answers1

0

Your code is trying to find the file pages/admin.php in the C:\xampp\php\PEAR directory, which is on the include path. What I would probably do is to provide the absolute path form within the spl_autoload_register - something along these lines:

spl_autoload_register(function($class){
    $path = realpath(implode(DIRECTORY_SEPARATOR, [
        __DIR__,
        'pages',
        $class.'.php'
    ]));
    require_once($path);
});

Make sure that you point to the correct directory - by just using __DIR__ constant it will assume your pages directory is within the same directory as this autoloading file.

If you have it one level up, create your path as:

$path = realpath(implode(DIRECTORY_SEPARATOR, [
    __DIR__,
    '..',
    'pages',
    $class.'.php'
]));
Sebastian Sulinski
  • 5,815
  • 7
  • 39
  • 61