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


Get current file name after include in PHP

让我们假设我有以下内容class.wapper.php

// loop through events/ directory ($eventFile = filename of parsed file)
include $eventFile;

myevent.php

echo __FILE__;

myotherevent.php

echo __FILE__;

这两个echo都返回相同的东西:class.wapper.php,有没有办法获得真正的文件名?不是包含代码的那个?

class.wraper是加密API的一部分,它允许用户在给定目录中定义自定义php文件,以便在主应用程序中激发给定事件时调用用户定义的代码,因此我无法编辑主类,我只能通过编辑"事件"文件

p>尝试以下函数
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;

如果您想获得真实名称,例如访问该文件的完整路径,请使用函数realpath("yourfile.extension")

我给你举一个简短的例子:

假设我有一个名为index.php的文件。我想显示它的完整路径。这是这样做的代码:

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

在我的示例中,我将文件保存在C:''examplep''htdocs''test''index.php中。结果将显示这个确切的路径。

希望能有所帮助。