相对包含文件 [英] Relative include files
问题描述
我有一个文件
workers/activity/bulk_action.php,其中包含一个文件
workers/activity/bulk_action.php which includes a file
include('../../classes/aclass.php');
在aclass.php中,它确实:
Inside aclass.php it does:
include ('../tcpdf/config/lang/eng.php');
第二个文件中的include似乎正在使用第一个文件的工作目录,而不是相对于其自身,从而导致错误.如何运作?
It seems that the include in the second file is using the first files working directory instead of being relative to itself, resulting in an error. How does this work?
推荐答案
您可以使用以下命令修改第二个include
:
You can adapt the second include
with:
include (__DIR__.'/../tcpdf/config/lang/eng.php');
魔术常数__DIR__
指向当前的.php文件,并在其后附加相对路径,将导致正确的位置.
The magic constant __DIR__
refers to the current .php file, and by appending the relative path after that, will lead to the correct location.
但是它仅自PHP5.3起可用,如果需要与旧设置兼容,则必须使用dirname(__FILE__)
构造.
But it only works since PHP5.3 and you would have to use the dirname(__FILE__)
construct instead if you need compatibility to older setups.
这篇关于相对包含文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!