-->

获取调用的文件名从包括()(Get calling file name from include()

2019-06-23 20:38发布

我想,其中包括从包含文件中其他文件的文件名。

我知道还有就是__FILE__魔术常量,但没有帮助,因为它返回所包含的文件的名称,而不是其中包括

有没有办法做到这一点? 或者是不可能的,因为路上PHP解释?

Answer 1:

这实际上是一个什么样的PHP模板引擎做的只是一个特例。 考虑具有这种功能:

function ScopedInclude($file, $params = array())
{
    extract($params);
    include $file;
}

然后A.php可包括C.php是这样的:

<?php
// A.php
ScopedInclude('C.php', array('includerFile' => __FILE__));

此外, B.php可以包括C.php无故障的方法相同。

<?php
// B.php
ScopedInclude('C.php', array('includerFile' => __FILE__));

C.php可以知道通过看$ params数组在其包入。

<?php
// C.php
echo $includerFile;


Answer 2:

所以这个问题是很老,但我一直在寻找的答案,离开这里后不满意,我碰到$_SERVER['SCRIPT_FILENAME']; 当然这工作,如果PHP文件做的,包括是一个网页。

这给你服务器上的“包括文件”的完整路径。 例如/var/www/index.php。 所以如果你想只是文件名,例如index.php文件,你需要使用基名()如

basename($_SERVER['SCRIPT_FILENAME']);

所以,如果在你的index.php,你有下面这行:

<?php include("./somephp.php"); ?>

在somephp.php你有

echo "this is the file that included me: " . basename($_SERVER['SCRIPT_FILENAME']);

你会得到

this is the file that included me: index.php

输出到浏览器。 这也适用,如果用户访问您的文件,而不明确包括在URL中的文件名,例如www.example.com ,而不是www.example.com/index.php



Answer 3:

明知用于包含文件的功能includeinclude_oncerequirerequire_once ,也知道他们被保留在PHP的话,这意味着它是不可能使用相同的名称来声明用户功能,并根据韦奇伍德的想法使用debug_backtrace你其实可以从哪些文件中包括被称为锻炼。

我们现在要做的是遍历回溯,直到我们找到了最近一次调用任意四种包括功能,以及它被称为文件。 下面的代码demostrate的技术:

function GetIncludingFile()
{
    $file = false;
    $backtrace =  debug_backtrace();
    $include_functions = array('include', 'include_once', 'require', 'require_once');
    for ($index = 0; $index < count($backtrace); $index++)
    {
        $function = $backtrace[$index]['function'];
        if (in_array($function, $include_functions))
        {
            $file = $backtrace[$index]['file'];
            break;
        }
    }
    return $file;
}

上面的代码将返回后,其中包括发生在文件的绝对路径,如果没有发生过任何包括它会返回false。 需要注意的是可以一直将文件从一个从另一个文件包含的文件包括,上述功能仅适用于最深包括。

通过简单的修改,你也可以得到最后一个包含文件:

function GetIncludedFile()
{
    $file = false;
    $backtrace =  debug_backtrace();
    $include_functions = array('include', 'include_once', 'require', 'require_once');
    for ($index = 0; $index < count($backtrace); $index++)
    {
        $function = $backtrace[$index]['function'];
        if (in_array($function, $include_functions))
        {
            $file = $backtrace[$index - 1]['file'];
            break;
        }
    }
    return $file;
}

意见

需要注意的是__FILE__不包含的文件,但当前文件。 例如:

文件a.php只会:

<?php
    function callme()
    {
        echo __FILE__;
    }
?>

文件B.php:

<?php
    include('A.php');
    callme();
?>

index.php文件:

<?php
    include('B.php');
?>

在上述例子中,在文件B.php所包含的文件的上下文中是B.php(以及包括文件是的index.php),但该函数的输出callme是a.php只会的路径,因为__FILE__是在甲.PHP。


另外请注意, $_SERVER['SCRIPT_FILENAME']将给予客户端请求的脚本的绝对路径。 如果$_SERVER['SCRIPT_FILENAME'] == __FILE__这意味着当前的文件是要求之一,因此有可能一直没有任何包括...

上述方法检查如果当前文件是所请求的一个,但如果未包括它(下面是所需的文件如何被包括的示例)。 一个实际的解决方案,以检查是否有一直没有任何夹杂物可能是检查count(get_included_files()) == 1


所请求的文件可以通过以下方式所包含的文件:

文件x.php

<?php
   $var = 'something';
   include('index.php');
?>

index.php文件

<?php
    if (!isset($var))
    {
        include('x.php');
        exit;
    }
    echo 'something';
?>

在上面的例子中,文件的index.php将包括x.php,然后x.php将包括的index.php回来,事后的index.php输出“东西”,并将控制返回到x.php,x.php将控制返回到索引.php然后到达出口;

这表明,即使是index.php的请求的脚本,它也从另一个脚本包括在内。



Answer 4:

我找不到简单的方法来掩盖这一点。 但是,如果包括一个对你很重要,你可以用一些全局变量破解它和你自己的include函数。

<?php                                                                            

    $g_including_files = array();                                                    

    function my_include($file) {                                                     
        $bt =  debug_backtrace();

        global $g_including_files;                                                   
        $g_including_files[basename($file)] = $bt[0]['file']; 
        return include($file);                                                                                                                                                                     
    } 

可以说是对你有帮助:)



文章来源: Get calling file name from include()