为什么在尝试 运行 这段代码时得到 "Warning: require_once(config): failed to open stream: No such file or directory"?
Why do I get "Warning: require_once(config): failed to open stream: No such file or directory" when trying to run this code?
我目前正在使用 PHP
使用 NetBeans IDE 8.0.2
对我自己的在线商店进行编程。我的系统是 Windows 7 32 位,我的本地主机由 WampServer 2.5 提供支持。我正在学习 THC 课程:https://www.youtube.com/playlist?list=PLbXVpMmmrntAvOYgkqhHW0hVu8dWUNyfz
到目前为止一切都很顺利,但我在这个视频中得到了股票:S2 {Building Framework} Class 和方法 (p6)。这家伙要求在屏幕上回显示例文本以测试代码,但是当 运行 本地主机上的项目时我收到这两条错误消息:
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()
)));
改变这个:
require_once('config');
至:
require_once('config.php');
//^^^See here file extension
(还要确保和autoload.php
在同一个目录下,否则换个路径)
编辑:
或者尝试使用这样的绝对路径:
require_once(dirname(__FILE__) . "/config.php");
编辑 2:
由于您现在从配置文件中收到错误消息,这意味着它已包含在内,但其中仍有一些错误!
第一个是这样的:
//directory seperator
defined("DS")
||define("DS", DIRECTORY_SEPERATOR);
//^^^^^^^^^^^^^^^^^^^ Typo must be: DIRECTORY_SEPARATOR
下一个在这里:
//classes folder
defined("CLASSES_DIR")
||define("CLASSES_DIR", classes);
//^^^^^^^ This isn't a constant so if it is a string put quotes around it
同样的错误:
//pages folder
defined("PAGES_DIR")
||define("PAGES_DIR", pages);
//^^^^^
下一个错误在这里:
//emails path
defined("EMAILS_PATH")
||define("EMAILS_PATH", ROOTH_PATH . DS . "emails");
//^^^^^^^^^^ Typo must be: ROOT_PATH , you have one h too much
这里也一样:
//catalogue images path
defined("CATALOGUE_PATH")
||define("CATALOGUE_PATH", ROOTH_PATH.DS. "media" .DS."catalogue");
//^^^^^^^^^^
到处都有 6 个错别字:
//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()
)));
编辑 3:
在这里你可以简化这两行,我会改变要求,所以即使你将文件本身包含到另一个文件中它也能工作!像这样:
autoload.php:
function __autoload($class_name) {
$class = explode("_", $class_name);
$path = implode("/", $class).".php";
require_once($path);
}
对此:
function __autoload($class_name) {
$path = str_replace("_", "/", $class_name) . ".php";
require_once(dirname(__FILE__) . "/" . $path);
}
我目前正在使用 PHP
使用 NetBeans IDE 8.0.2
对我自己的在线商店进行编程。我的系统是 Windows 7 32 位,我的本地主机由 WampServer 2.5 提供支持。我正在学习 THC 课程:https://www.youtube.com/playlist?list=PLbXVpMmmrntAvOYgkqhHW0hVu8dWUNyfz
到目前为止一切都很顺利,但我在这个视频中得到了股票:S2 {Building Framework} Class 和方法 (p6)。这家伙要求在屏幕上回显示例文本以测试代码,但是当 运行 本地主机上的项目时我收到这两条错误消息:
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()
)));
改变这个:
require_once('config');
至:
require_once('config.php');
//^^^See here file extension
(还要确保和autoload.php
在同一个目录下,否则换个路径)
编辑:
或者尝试使用这样的绝对路径:
require_once(dirname(__FILE__) . "/config.php");
编辑 2:
由于您现在从配置文件中收到错误消息,这意味着它已包含在内,但其中仍有一些错误!
第一个是这样的:
//directory seperator
defined("DS")
||define("DS", DIRECTORY_SEPERATOR);
//^^^^^^^^^^^^^^^^^^^ Typo must be: DIRECTORY_SEPARATOR
下一个在这里:
//classes folder
defined("CLASSES_DIR")
||define("CLASSES_DIR", classes);
//^^^^^^^ This isn't a constant so if it is a string put quotes around it
同样的错误:
//pages folder
defined("PAGES_DIR")
||define("PAGES_DIR", pages);
//^^^^^
下一个错误在这里:
//emails path
defined("EMAILS_PATH")
||define("EMAILS_PATH", ROOTH_PATH . DS . "emails");
//^^^^^^^^^^ Typo must be: ROOT_PATH , you have one h too much
这里也一样:
//catalogue images path
defined("CATALOGUE_PATH")
||define("CATALOGUE_PATH", ROOTH_PATH.DS. "media" .DS."catalogue");
//^^^^^^^^^^
到处都有 6 个错别字:
//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()
)));
编辑 3:
在这里你可以简化这两行,我会改变要求,所以即使你将文件本身包含到另一个文件中它也能工作!像这样:
autoload.php:
function __autoload($class_name) {
$class = explode("_", $class_name);
$path = implode("/", $class).".php";
require_once($path);
}
对此:
function __autoload($class_name) {
$path = str_replace("_", "/", $class_name) . ".php";
require_once(dirname(__FILE__) . "/" . $path);
}