获取运行PHP包含的文件的文件名 [英] Get filename of file which ran PHP include
本文介绍了获取运行PHP包含的文件的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用PHP include
时,如何找出调用 include
的文件?简而言之,父文件的文件名是什么?
When using the PHP include
, how can I find out which file is calling the include
? In short, what is the parent's file filename?
推荐答案
一种简单的方法是在父文件中分配一个变量(在包含),然后在包含的文件中引用该变量。
An easy way is to assign a variable in the parent file (before the inclue), then reference that variable in the included file.
父文件:
$myvar_not_replicated = __FILE__; // Make sure nothing else is going to overwrite
include 'other_file.php';
包含文件:
if (isset($myvar_not_replicated)) echo "{$myvar_not_replicated} included me";
else echo "Unknown file included me";
您还可以使用 get_included_files()
或 debug_backtrace()
并查找包含文件的时间和地点,但这可能会有点麻烦和复杂。
You could also mess around with get_included_files()
or debug_backtrace()
and find the event when and where the file got included, but that can get a little messy and complicated.
这篇关于获取运行PHP包含的文件的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文