Get filename of file which ran PHP include(获取运行 PHP 的文件的文件名包括)
问题描述
当使用 PHP include
时,我如何知道是哪个文件调用了 include
?简而言之,父文件filename是什么?
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 的文件的文件名包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:获取运行 PHP 的文件的文件名包括


基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01