1

I'm currently working on programming my very own online store with NetBeans IDE 8.0.2 using PHP. My system is Windows 7 32bit and my localhost is powered by WampServer 2.5. I'm following THC Courses: https://www.youtube.com/playlist?list=PLbXVpMmmrntAvOYgkqhHW0hVu8dWUNyfz

So far everything was going great but I got stock at this video: S2 {Building Framework} Class and method (p6). The guy is asking to echo a sample text on the screen to test the code, but I get these two error messages when running the project on localhost:

Warning: require_once(config): failed to open stream: No such file or directory in C:\wamp\www\ecommerce\inc\autoload.php on line 2
Fatal error: require_once(): Failed opening required 'config' (include_path='.;C:\php\pear') in C:\wamp\www\ecommerce\inc\autoload.php on line 2

autoload.php:

<?php

    require_once('config');

    function __autoload($class_name) {

        $class = explode("_", $class_name);
        $path = implode("/", $class).".php";
        require_once($path);

    }

Core.php:

<?php

    class Core {

        public function run() {
            echo "Hello this is a print test";

        }

    }

index.php:

<?php

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

config.php:

<?php

    if(!isset($_SESSION)) {
        session_start();

    }

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

    //directory seperator
    defined("DS")
    ||define("DS", DIRECTORY_SEPERATOR);

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


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

    //pages folder
    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", ROOTH_PATH.DS. "emails");

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


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

    )));
Rizier123
  • 58,877
  • 16
  • 101
  • 156
user23524697
  • 137
  • 1
  • 6
  • 18

1 Answers1

9

Change this:

require_once('config');

to:

require_once('config.php');
                   //^^^See here file extension

(Also make sure it's in the same directory with autoload.php, otherwise change the path)

EDIT:

Or try i with a absolute path like this:

require_once(dirname(__FILE__) . "/config.php");

EDIT 2:

Since you now get error messages from the config file, means that it got included, but still has some errors in it!

The first would be this:

//directory seperator
defined("DS")
||define("DS", DIRECTORY_SEPERATOR);
             //^^^^^^^^^^^^^^^^^^^ Typo must be: DIRECTORY_SEPARATOR

Next one is here:

//classes folder
defined("CLASSES_DIR")
||define("CLASSES_DIR", classes);
                      //^^^^^^^ This isn't a constant so if it is a string put quotes around it

Same error here:

//pages folder
defined("PAGES_DIR")
||define("PAGES_DIR", pages);
                    //^^^^^

Next error here:

//emails path
defined("EMAILS_PATH")
||define("EMAILS_PATH", ROOTH_PATH . DS .  "emails");
                      //^^^^^^^^^^ Typo must be: ROOT_PATH , you have one h too much

Same here:

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

And all over the palce you have 6 typos here:

//add all above directories to the include path
set_include_path(implode(PATH_SEPERATOR, array(
                       //^^^^^^^^^^^^^^ Typo must be: PATH_SEPARATOR 
realpath(ROOTH_PATH.DS.CLASSES_DIR),
       //^^^^^^^^^^ Typo must be: ROOT_PATH , you have one h too much
realpath(ROOTH_PATH.DS.PAGES_DIR),
       //^^^^^^^^^^
realpath(ROOTH_PATH.DS.MOD_DIR),
       //^^^^^^^^^^
realpath(ROOTH_PATH.DS.INC_DIR),
       //^^^^^^^^^^
realpath(ROOTH_PATH.DS.TEMPLATE_DIR).
       //^^^^^^^^^^
get_include_path()

)));

EDIT 3:

Here you can simplify these 2 lines and i would change the require, so it works even if you include the file itself into another one! Like this:

autoload.php:

function __autoload($class_name) {

    $class = explode("_", $class_name);
    $path = implode("/", $class).".php";
    require_once($path);

}

to this:

function __autoload($class_name) {

    $path = str_replace("_", "/", $class_name) . ".php";
    require_once(dirname(__FILE__) . "/" . $path);

}
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • Now that I've changed it, I receive 10 fatal errors instead of 2... :S – user23524697 Jan 12 '15 at 19:36
  • 1
    @MebZone Is the file in the same directory and or changed you the the paht? What errors do you get? – Rizier123 Jan 12 '15 at 19:37
  • Thanks for helping me! I tried with an absolute path and same error messages. The first 8 errors are like this. Notice: Use of undefined constant ROOTH_PATH - assumed 'ROOTH_PATH' in C:\wamp\www\ecommerce\inc\config.php on line 57 But when I change it in NetBeans, of course NetBeans give me a syntax error. – user23524697 Jan 12 '15 at 19:46
  • @MebZone But that means your include works, because now you can get error messages from there. – Rizier123 Jan 12 '15 at 19:47
  • Ok. What do you suggest? the last two messages now deal with Core.php Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\wamp\www\ecommerce\inc\autoload.php on line 7 Fatal error: require_once(): Failed opening required 'Core.php' (include_path='PATH_SEPERATORPATH_SEPERATORPATH_SEPERATORPATH_SEPERATOR.;C:\php\pear') in C:\wamp\www\ecommerce\inc\autoload.php on line 7 Core.php is there.. I don't understand – user23524697 Jan 12 '15 at 19:52
  • Thanks for helping me. I work too late! Excuse my English... My first language is french... but now that I changed all the typos I still receive two error messages: Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\wamp\www\ecommerce\inc\autoload.php on line 7 Fatal error: require_once(): Failed opening required 'Core.php' (include_path=';;;;.;C:\php\pear') in C:\wamp\www\ecommerce\inc\autoload.php on line 7 – user23524697 Jan 12 '15 at 20:05
  • @MebZone Is `core.php` in the same directory as `autload.php`(->`inc/`) ? Or where is it located? – Rizier123 Jan 12 '15 at 20:06
  • Ayayaye. No it is not... because following the courses the guy said to put core.php in the classes folder... See it here at 2:50 - https://www.youtube.com/watch?v=7Nm0TzyGHaE&list=PLbXVpMmmrntAvOYgkqhHW0hVu8dWUNyfz&index=7 – user23524697 Jan 12 '15 at 20:16
  • @MebZone Then try with this line to include the file: `require_once("../classes/" . $path;)` – Rizier123 Jan 12 '15 at 20:20
  • I have hope that we will get to the bottom of this problem... I followed your suggestion just now and unfortunately I still receive Warning: require_once(C:\wamp\www\ecommerce\inc/Core.php): failed to open stream: No such file or directory in C:\wamp\www\ecommerce\inc\autoload.php on line 7 Fatal error: require_once(): Failed opening required 'C:\wamp\www\ecommerce\inc/Core.php' (include_path=';;;;.;C:\php\pear') in C:\wamp\www\ecommerce\inc\autoload.php on line 7 – user23524697 Jan 12 '15 at 20:28
  • What is pear anyway? Do I need to install it? – user23524697 Jan 12 '15 at 20:29
  • @MebZone Don't bother about pear, sry try this: `require_once("classes/" . $path);` – Rizier123 Jan 12 '15 at 20:32
  • Rizier123 thanks a lot mate! Working fine now! I can see the print test! Have an awesome day :) – user23524697 Jan 12 '15 at 20:35
  • There is now a troubleshooting checklist for this frequent error here : stackoverflow.com/a/36577021/2873507 – Vic Seedoubleyew Apr 12 '16 at 16:50
  • 1
    @VicSeedoubleyew Thanks for the comment :) I will look at, maybe I can also improve it, and if it is a good one and fits this question I can close it as a dupe. – Rizier123 Apr 12 '16 at 17:10