在PHP中包含后获取当前文件名

在PHP中包含后获取当前文件名,php,include,Php,Include,让我们假装我有以下内容 class.wrapper.php // loop through events/ directory ($eventFile = filename of parsed file) include $eventFile; echo __FILE__; echo __FILE__; myevent.php // loop through events/ directory ($eventFile = filename of parsed file) include $

让我们假装我有以下内容 class.wrapper.php

// loop through events/ directory ($eventFile = filename of parsed file)
include $eventFile;
echo __FILE__;
echo __FILE__;
myevent.php

// loop through events/ directory ($eventFile = filename of parsed file)
include $eventFile;
echo __FILE__;
echo __FILE__;
myotherevent.php

// loop through events/ directory ($eventFile = filename of parsed file)
include $eventFile;
echo __FILE__;
echo __FILE__;
两个echo都返回相同的内容:class.wrapper.php,有没有办法获取真实的文件名?不是包含代码的那个


class.wrapper是一个加密API的一部分,它允许用户在给定目录中定义自定义php文件,以便在主应用程序中触发给定事件时调用用户定义的代码,因此我无法编辑主类,如果要获得真实名称,我只能通过编辑“事件”文件来完成此操作,对于您的示例,这是访问此文件的完整路径,请使用函数
realpath(“yourfile.extension”)

我举一个简短的例子:

假设我有一个名为index.php的文件。我想显示它的完整路径。以下是执行此操作的代码:

<?php
    echo realpath("index.php");
?>

在我的示例中,我将文件保存在C:\xampp\htdocs\test\index.php中。结果将显示此确切路径


希望有帮助。

试试下面的功能

function GetInclude($file, $params = array())
    {
        extract($params);
        include $file;
    }
    myevent.php
    <?php GetInclude('class.wrapper.php', array('includerFile' => __FILE__)); ?>
    myotherevent.php
    <?php GetInclude('class.wrapper.php', array('includerFile' => __FILE__)); ?>
    class.wrapper.php
    echo $includerFile;
函数GetInclude($file,$params=array()) { 摘录($params); 包括$file; } myevent.php myotherevent.php class.wrapper.php echo$includerFile;
奇怪。根据文件的文档,magic常量应该返回包含文件的名称。这也是我的想法,问题是我使用的是第三方代码(加密),它允许我在触发这些事件时填充这些事件的php文件以执行第三方代码,所以我不知道它们是否使用了include,但是看起来最合乎逻辑的事情是我不能编辑包装器类,它是一个加密的API,在给定的目录中包含一些文件,这个API来自哪个API?有文件吗?让我知道这一点,以便尝试查找类似上述的函数。我不认为light doc会对您有所帮助,但事实上,在这里很难知道我需要搜索哪种情况。。但如果我理解正确的话,您的文件名包含在$eventFile变量中。即使echo realpath($eventFile)也会返回“class.wrapper.php”?在任何情况下,我都需要知道你的事件,如果你想让我的研究变得更容易,我唯一能做的就是,它是一个(许多)空的php文件。在触发事件时包含此文件,我不介意何时发生。在这个文件中,我想找到文件名,使用file不起作用(返回包含当前文件的类文件的名称)