Php 设置包含路径时出错

Php 设置包含路径时出错,php,require,dir,Php,Require,Dir,这是我正在使用的代码(它是index.php文件) 我正在开发一个具有这种结构的框架 application/ public/ index.php system/ 我想设置根/(包含application/和system/的文件夹)的include路径\uuuu DIR\uuuu给了我很多东西,比如Application/xammp/htdocs/Application/public/index.php(在localhost中);考虑到我一开始对路径一无所知,我不确定客

这是我正在使用的代码(它是index.php文件)

我正在开发一个具有这种结构的框架

application/
    public/
        index.php
system/
我想设置根/(包含application/和system/的文件夹)的include路径
\uuuu DIR\uuuu
给了我很多东西,比如
Application/xammp/htdocs/Application/public/index.php
(在localhost中);考虑到我一开始对路径一无所知,我不确定客户机
\uuuuu DIR\uuuuu
是否有如此大的不同。我只是写了前几行,以便轻松地从DIR中删除最后两个文件夹,因此我确信无论
\uuuuuu DIR\uuuu
是什么,路径都是正确的。我在另一个测试环境中测试了这些线路,它们工作正常


发生的奇怪的事情是,如果我按代码显示的那样运行代码,它会给我“错误”。而如果我在检查它的存在之前只需要
system/base/file.php
,它就可以工作。因此,如果我取消注释(***1),该文件是必需的。

您可以使用$path=dirname(dirname(DIR),这将实现与前5行代码相同的功能

根据PHP文档,include_path仅适用于:require()、include()、fopen()、file()、readfile()和file_get_contents(),而不是file_exists()。因此,必须将$path存储在变量或常量中。例如:

/* Set an include path
*/
$path = dirname(dirname(__DIR__));
ini_set('include_path', $path);
define('PATH', $path)

// require('system/base/file.php'); // ***1

/* Starter file
*/
if (file_exists(PATH . 'system/base/file.php')) { require('system/base/file.php'); }
else { exit('Error'); }

我该怎么处理这个问题呢?此时设置ini包含路径配置毫无意义。事实上,使用ini包含路径配置现在有点多余。也许您应该只需要()所需的文件,并使用set_error_handler定义一个错误处理程序,用于自定义错误管理。感谢dirname()和建议。我完全忘记了dirname()函数。:)
/* Set an include path
*/
$path = dirname(dirname(__DIR__));
ini_set('include_path', $path);
define('PATH', $path)

// require('system/base/file.php'); // ***1

/* Starter file
*/
if (file_exists(PATH . 'system/base/file.php')) { require('system/base/file.php'); }
else { exit('Error'); }