2015-04-03 80 views
0

我有一个使用require_once的问题。 我指定了错误的路径,找不到解决方案。使用require_once的错误路径

我有一个名为header.php的文件,它使用require_once:functions.php和navigation.php包含两个文件。 这部分工作正常。 当我尝试将header.php包含在位于不同目录中的名为view.php的文件中时,会出现此问题。

这里是树状结构:

C:\wamp\www\1.1\plugins\docreader\php\view.php 
C:\wamp\www\1.1\theme\dark-blue\templates\files\header.inc.php 
C:\wamp\www\1.1\theme\dark-blue\templates\files\functions.inc.php 
C:\wamp\www\1.1\theme\dark-blue\templates\files\navigation.inc.php 

我尝试了很多不同的路径,但没有成功。

请问有人有线索吗?

+1

你只需要一点['MAGIC'](http://php.net/manual/en/language.constants.predefined.php) – Rizier123 2015-04-03 17:41:14

+2

很高兴我的php日子已经结束xD – iamwhitebox 2015-04-03 17:42:24

回答

0

你需要让你的文件路径是绝对的而不是相对的。一个有用的超级全球实现这个是$_SERVER['DOCUMENT_ROOT']这将评估您的Web服务器文件根目录。

+0

谢谢,我会尝试。 – 2015-04-03 18:13:49

1

阅读(和使用)魔术常数__DIR__和函数dirname()以从包含器的路径开始生成包含文件的路径。

例如,如果在plugins\docreader\php\view.php要包括theme\dark-blue\templates\files\functions.inc.php然后使用这样的事情:

// Use this in 'plugins\docreader\php\view.php' 
include dirname(dirname(dirname(__DIR__))). 
     '/theme/dark-blue/templates/files/functions.inc.php'; 

__DIR__magic constant计算结果为包含在那里使用的文件的目录。在C:\wamp\www\1.1\plugins\docreader\php\view.php中,__DIR__的值是'C:\wamp\www\1.1\plugins\docreader\php'

函数dirname()返回提供的路径的父目录。那种..,只有更好。三次使用它将作为参数传递的值(上面解释的值__DIR__)减少到'C:\wamp\www\1.1'。一切都从这里直线前进:将相对路径添加到所需的文件('/theme/dark-blue/templates/files/functions.inc.php'),并忘记包含问题。

+0

感谢您提供答案的北京。 – 2015-04-03 17:57:05

+0

谢谢你的提示回答。我尝试了你的建议,但它不起作用。我认为问题来自于header.php包含两个其他文件。 header.php是我试图包含在view.php中的一个。我试图在view.php中包含其他两个,但问题仍然存在。 – 2015-04-03 18:13:03

+0

注意:使用未定义常量SITE_TEMPLATES_PATH - 在第3行C:\ wamp \ www \ 1.1 \ theme \ dark-blue \ templates \ files \ header.inc.php中假定为'SITE_TEMPLATES_PATH' 警告:require_once(SITE_TEMPLATES_PATH/files /第3行中C:\ wamp \ www \ 1.1 \ theme \ dark-blue \ templates \ files \ header.inc.php中没有这样的文件或目录 致命错误:require_once ():在C:\ wamp \ www \ 1.1 \ theme \ dark-blue \ templates \ files中打开所需的'SITE_TEMPLATES_PATH/files/functions.inc.php'(include_path ='.; C:\ php \ pear')失败\ 3号线上的\ header.inc.php – 2015-04-03 18:49:41