我有以下目录结构:
home/admin/super/
在home中,我有一个名为config.php的文件
<?php
//Get relative path - for PHP files
$documentRoot = $_SERVER['DOCUMENT_ROOT'].'/mwo2015/';
include($documentRoot.'library/constants.php');
echo $paypalID;
?>echo $paypalID得到了回响;
在路径home/admin/中,有一个名为access-control.php的文件
<?php
//Common files
include('../config.php');
?>回声$paypalID又一次被回响。
在path home/admin/super/中,有一个名为output.php的文件
<?php
include('../access-control.php');
?>没有输出,我在路径的error_log中得到以下错误
PHP Warning:  include() [<a href='function.include'>function.include</a>]: Failed opening '../config.php' for inclusion (include_path='.:/usr/lib64/php:/usr/share/pear')发布于 2015-11-16 11:00:37
试试这个..。
“相对包含路径”不会移到包含的文件中。
解决方案是使用绝对路径,您可以使用dirname(__FILE__)与亲属和绝对路径组合,并获得包含该文件的目录。
在包含文件中,可以使用以下内容
require_once dirname(__FILE__) . '/yourfile.php';
require_once dirname(__DIR__) . '/yourfile.php';或
include dirname(__FILE__) . "/yourfile.php";
include dirname(__DIR__) . "/yourfile.php";发布于 2015-11-16 11:42:00
你遇到的问题是:
相对路径使用入口点。因此,如果您开始并访问home/admin/super/output.php,则从那里开始。
所以家/管理员/超级。
include('../access-control.php'); 回家/管理
这个文件中的包含会转到home/admin,但是它应该返回到家里,因此要修复这个问题,您可以这样做:
../../config.php这就是为什么要围绕一个入口点构建应用程序,或者创建一个全局根和webroot,以便在整个应用程序中使用。
https://stackoverflow.com/questions/33733634
复制相似问题