2011-09-17 93 views
0

我在同一个目录中使用require_once()或include_once()时没有任何问题。但如果我使用如下:Require_once()和include_once()失败

我有一个php类的页面名称email.class.php其中我已包括下面的代码。

<?php 
require_once('../../configuration.php'); //The file is outside of directory. (root/system/class/) where the configuration.php file is not in the class directory but in root directory 
?> 

现在我已经在email.class.php包括象下面这样认购表格。

<?php 
require_once('../class/email.class.php'); // Directory structure is like: (root/system/package/) // the formprocess.php is in the package folder. 
?> 

看来一切正确。但是当我尝试处理表单时,致命错误: require_once无法打开所需的../../configuration.php文件。 然后我试着用URL代替'../..configuration.php'。但我的主机不允许我打开URL。哪里有问题 ?请帮助我的任何人。我只是一个学习者。

+0

见'GETCWD()'和'真实路径()' – chelmertz

回答

1

始终使用完整路径类似require_once(dirname(__FILE__) . '/../../configuration.php');

或者require_once(__DIR__ . '/../../configuration.php'),如果你的PHP版本> = 5.3

+0

我的php版本是5.2 –

+0

因此,使用前一个。 – xdazz

+0

谢谢,这个解决方案对我来说非常合适。我有一个小问题?为什么你使用了** dirname(__ FILE __)。'/ ** <---结尾的斜杠('/'在'../'之前)。意味着你为什么写'/../../configuration.php insted'../../configuration.php'。我不知道这件事。 –

0

可能的原因:

  • 你不要让你的Web服务器访问特定的目录
  • 的目录路径错误(样式:..对于文件夹/文件夹名称,取决于当前处理位置

检查更多的相关信息的帮助.. http://php.net/manual/en/function.require-once.php