Why does this PHP relative include fail?

一笑奈何 提交于 2019-12-01 22:13:22

paths are always relative to the script which got called. in your example c.php is loaded because "." (current directory) is always in the include_path.

to fix this you can use dirname(__FILE__) to always know the directory of the file itself. (the file in which you write FILE)

or you can use dirname($_SERVER['SCRIPT_FILENAME']) to alwys get the directory of the caling script.

As you're starting with a.php, you should define the include directories in a.php:

define('MY_INCLUDES', dirname(__FILE__) . '/include/');
define('MY_DATA', dirname(__FILE__) . '/data/');

Afterwards include the files with absolute paths:

include(MY_INCLUDES . 'b.php');
include(MY_DATA . 'c.php');
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!