dongmen1925 2015-01-12 19:17
浏览 298
已采纳

为什么我在尝试运行此代码时会收到“警告:require_once(config):无法打开流:没有这样的文件或目录”?

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()

    )));
  • 写回答

1条回答 默认 最新

  • dongzanghua8422 2015-01-12 19:18
    关注

    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);
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵