1

我正在使用此代码来定义我的项目的目录路径:

config.php 是

<?php
define('CB_HOME', realpath(dirname(__DIR__)));
define('FD_HOME', CB_HOME."/testbot");

我在其他文件中使用了 CB_home 和 FD_home。

我收到错误:

PHP Notice:  Use of undefined constant CB_HOME - assumed 'CB_HOME' in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3
PHP Warning:  require_once(CB_HOME/AbstractCbRest.php) [<a href='function.require-once'>function.require-once</a>]: failed to open stream: No such file or directory in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3
 PHP Fatal error:  require_once() [<a href='function.require'>function.require</a>]: Failed opening required 'CB_HOME/AbstractCbRest.php' (include_path='.;C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\test-bot\\testbot\\bootstrap.php on line 3

我的 bootstrap.php 是:

<?php 
require_once 'config.php';
require_once CB_HOME.'/AbstractCbRest.php';

哪里出了问题?

4

2 回答 2

1

我的猜测是include_path设置干扰了您的脚本。所以(正如@deceze 提到的)你可能正在加载一个不正确的(虽然存在)config.php 文件。

也许您可以尝试使用绝对路径来包含您的配置或使用以下内容:

require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'config.php';
于 2012-05-22T08:40:58.087 回答
-1

很明显,带有两个“\”的windows路径不起作用。要删除双反斜杠(注意:这不是解决方案,而是一种解决方法),请执行以下操作:

$someValue = 'C:\\xampp\\htdocs\\test-bot\\testbot\\'; $someValue = preg_replace('/\\\\/u', '\\', $someValue);

或者在你的情况下:

define('CB_HOME', preg_replace('/\\\\/u', '\\', realpath(dirname(DIR))));
于 2012-05-22T08:28:02.360 回答