PHP包含路径错误 [英] PHP Include Path Error
问题描述
基本上我正在学习php我对程序性PHP有很好的了解,现在我正在使用Udemy.com教程学习OOPS并使用Framework,所以我跟随我的Udemy老师他在做什么,我认为导师正在做完美的编码,因为当我在我的托管上运行编码工作正常,所以我分享我的整个编码请检查并告诉我解决方案,唯一的问题是,当我试图访问我的index.php文件以下发生错误。
我的项目路径:C:// xampp / htdocs / project-name
警告:require_once(Core.php):无法打开流:没有这样的文件或目录中的class =code-keyword>在 7 $上的C:\ xampp \\\\\\ b
$ b致命错误:require_once():无法打开所需' Core.php' (一世nclude_path = ' 。; C:\ xampp \ php \ PEAR') 7 $的 C:\ xampp \\\\\\\\\\\\ p $ p>
项目编码:
1。 /inc/autoload.php
<?php
require_once(' config.php');
函数__autoload($ class_name)
{
$ class = explode( _,$ class_name);
$ path = implode( / ,$ class)。 。php;
require_once($ path);
}
2。 /inc/config.php
<?php
if(!isset($ _ SESSION)){
session_start();
}
// 网站域名为http $ b定义$ b( SITE_URL)
|| define( SITE_URL, < span class =code-string> https://。$ _ SERVER [' SERVER_NAME'< /跨度>]);
// 目录分隔符
defined( DS)
|| define( DS,DIRECTORY_SEPARATOR);
// root path
defined( ROOT_PATH)
|| define( ROOT_PATH,realpath(dirname(__ FILE__)。DS。 .. .DS));
// classes文件夹
defined( CLASSES_DIR)
|| define( CLASSES_DIR, < span class =code-string> classes);
// pages directory
defined( PAGES_DIR)
|| define( PAGES_DIR, < span class =code-string> pages);
// modules文件夹
defined( MOD_DIR)
|| define( MOD_DIR, < span class =code-string> mod);
// inc文件夹
defined( INC_DIR)
|| define( INC_DIR, < span class =code-string> inc);
// templates文件夹
defined( TEMPLATE_DIR)
|| define( TEMPLATE_DIR, < span class =code-string> template);
// 电子邮件路径
defined( EMAILS_PATH)
|| define( EMAILS_PATH,ROOT_PATH.DS。 电子邮件);
// 目录图片路径
defined( CATALOGUE_PATH)
|| define( CATALOGUE_PATH,ROOT_PATH.DS。 media .DS。 catalog< /跨度>);
// 将以上所有目录添加到包含路径
ini_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),
ini_get(' include_path')
))) ;
3。 /classes/Core.php
<?php
class 核心
{
公共函数run()
{
ob_start();
require_once(Url :: getPage());
ob_get_flush();
}
}
4。 /classes/Url.php
<?php
class Url
{
public static $ _ page = page;
public static $ _ folder = PAGES_DIR;
public static $ _ prams = array();
公共静态函数getParam($ par)
{
return isset($ _ GET [$ par])&& $ _GET [$ par]!= ? $ _GET [$ par]: null ;
}
公共静态函数cPage() // 当前页面返回karta hai。
{
return isset($ _ GET [self :: $ _ page])? $ _GET [self :: $ _ page]:' index';
}
公共静态函数getPage()
{
$ page = self :: $ _ folder.DS.self :: cPage()。 。php;
$ error = self :: $ _ folder.DS。 error.php跨度>;
return is_file($ page)? $ page : $ error ;
}
公共静态函数getAll()
{
if(!empty($ _ GET))
{
foreach($ _ GET as $ key => $ value)
{
if(!empty($ value) )
{
self :: $ _ prams [$ key] = $ value ;
}
}
}
}
}
5。 /pages/index.php
<?php
require_once(' _ header.php');
require_once(' _ footer.php');
?>
6。 /pages/error.php
<?php
require_once(' _ header.php');
echo( < h1>错误< / h1>);
require_once(' _ footer.php');
?>
7。 index.php
<?php
require_once(' inc / autoload.php');
$ core = new Core();
$ core-> run();
?>
以上是当我运行此项目时整个项目的代码我得到以下错误,我如何解决,请帮助。
警告:require_once( Core.php):无法打开流: C:\ xampp \\中没有此类文件或目录 /htdocs \completed\inc\autoload.php在线 7
致命错误:require_once():无法打开所需' Core.php'(include_path = ' < span class =code-string> .; C:\ xampp \ php \ PEAR')在 C:\ xampp \ htdocs\completed\inc\autoload.php在线 7
解决方案
class_name)
{
class = explode( _跨度>,
CLASS_NAME);
Basically i am learning php i have great knowledge about procedural PHP, now i am using "Udemy.com" tutorials to learn OOPS and work with Framework, so i am following my Udemy teacher what he is doing, and i think the tutor is doing perfect coding because when i run that coding on my hosting that works fine, so i am sharing my whole coding please check and tell me the solution, The only problem is that when i tried to access my index.php file the following error occurs.
My Project Path: C://xampp/htdocs/project-name
Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7
Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7
Project Coding:
1. /inc/autoload.php
<?php
require_once('config.php');
function __autoload($class_name)
{
$class = explode("_",$class_name);
$path = implode("/",$class).".php";
require_once($path);
}
2. /inc/config.php
<?php
if(!isset($_SESSION)) {
session_start();
}
// site domain name with http
defined("SITE_URL")
|| define("SITE_URL", "https://".$_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
ini_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),
ini_get('include_path')
)));
3. /classes/Core.php
<?php
class Core
{
public function run()
{
ob_start();
require_once(Url::getPage());
ob_get_flush();
}
}
4. /classes/Url.php
<?php
class Url
{
public static $_page = "page";
public static $_folder = PAGES_DIR;
public static $_prams = array();
public static function getParam($par)
{
return isset($_GET[$par]) && $_GET[$par] != "" ? $_GET[$par] : null;
}
public static function cPage() // yeh current page return karta hai.
{
return isset($_GET[self::$_page]) ? $_GET[self::$_page] : 'index';
}
public static function getPage()
{
$page = self::$_folder.DS.self::cPage().".php";
$error = self::$_folder.DS."error.php";
return is_file($page) ? $page : $error;
}
public static function getAll()
{
if(!empty($_GET))
{
foreach($_GET as $key => $value)
{
if(!empty($value))
{
self::$_prams[$key] = $value;
}
}
}
}
}
5. /pages/index.php
<?php
require_once('_header.php');
require_once('_footer.php');
?>
6. /pages/error.php
<?php
require_once('_header.php');
echo("<h1>Error</h1>");
require_once('_footer.php');
?>
7. index.php
<?php
require_once('inc/autoload.php');
$core = new Core();
$core->run();
?>
Above are the codes for whole project when i run this i got following error, How can i resolve, Kindly Help.
Warning: require_once(Core.php): failed to open stream: No such file or directory in C:\xampp\htdocs\completed\inc\autoload.php on line 7
Fatal error: require_once(): Failed opening required 'Core.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\completed\inc\autoload.php on line 7
解决方案 class_name)
{
class = explode("_",
class_name);
这篇关于PHP包含路径错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!