获取包含PHP脚本的文件的名称

时间:2021-03-27 23:45:13

Here is an example of what I am trying to do:

这是我想要做的一个例子:

index.php

<ul><?php include("list.php") ?></ul>

list.php

<?php
    if (PAGE_NAME is index.php) {
        //Do something
    }
    else {
        //Do something
    }
?>

How can I get the name of the file that is including the list.php script (PAGE_NAME)? I have tried basename(__FILE__), but that gives me list.php.

如何获取包含list.php脚本(PAGE_NAME)的文件的名称?我试过basename(__ FILE__),但这给了我list.php。

6 个解决方案

#1


25  

$_SERVER["PHP_SELF"]; returns what you want

$ _SERVER [ “PHP_SELF”];返回你想要的

#2


5  

If you really need to know what file the current one has been included from - this is the solution:

如果你真的需要知道当前包含的文件是什么 - 这是解决方案:

$trace = debug_backtrace();

$from_index = false;
if (isset($trace[0])) {
    $file = basename($trace[0]['file']);

    if ($file == 'index.php') {
        $from_index = true;
    }
}

if ($from_index) {
    // Do something
} else {
    // Do something else
}

#3


5  

In case someone got here from search engine, the accepted answer will work only if the script is in server root directory, as PHP_SELF is filename with path relative to the server root. So the universal solution is

如果有人从搜索引擎到达此处,则只有当脚本位于服务器根目录中时,接受的答案才会起作用,因为PHP_SELF是具有相对于服务器根目录的路径的文件名。所以通用解决方案是

basename($_SERVER['PHP_SELF'])

Also keep in mind, that this returns the top script, for example if you have a script and include a file, and then in included file include another file and try this, you will get the name of the first script, not the second.

另外请记住,这会返回*脚本,例如,如果您有脚本并包含文件,然后在包含文件中包含另一个文件并尝试此操作,您将获得第一个脚本的名称,而不是第二个脚本。

#4


2  

In the code including list.php, before you include, you can set a variable called $this_page and then list.php can see the test for the value of $this_page and act accordingly.

在包含list.php的代码中,在包含之前,可以设置一个名为$ this_page的变量,然后list.php可以看到$ this_page的值的测试并相应地执行。

#5


2  

Perhaps you can do something like the following:

也许你可以做类似以下的事情:

<ul>
    <?php
        $page_name = 'index';
        include("list.php")
    ?>
</ul>

list.php

<?php
    if ($pagename == 'index') {
        //Do something
    }
    else {
        //Do something
    }
?>

#6


1  

The solution basename($_SERVER['PHP_SELF']) works but I recommend to put a strtolower(basename($_SERVER['PHP_SELF'])) to check 'Index.php' or 'index.php' mistakes.

该解决方案基本名($ _ SERVER [ 'PHP_SELF'])的作品,但我建议把一个用strtolower(基名($ _ SERVER [ 'PHP_SELF']))来检查 '的index.php' 或 '的index.php' 的错误。

But if you want an alternative you can do:
<?php if (strtolower(basename($_SERVER['SCRIPT_FILENAME'], '.php')) === 'index'): ?>.

但是如果你想要一个替代方案,你可以这样做:<?php if(strtolower(basename($ _ SERVER ['SCRIPT_FILENAME'],'。php'))==='index'):?>。

#1


25  

$_SERVER["PHP_SELF"]; returns what you want

$ _SERVER [ “PHP_SELF”];返回你想要的

#2


5  

If you really need to know what file the current one has been included from - this is the solution:

如果你真的需要知道当前包含的文件是什么 - 这是解决方案:

$trace = debug_backtrace();

$from_index = false;
if (isset($trace[0])) {
    $file = basename($trace[0]['file']);

    if ($file == 'index.php') {
        $from_index = true;
    }
}

if ($from_index) {
    // Do something
} else {
    // Do something else
}

#3


5  

In case someone got here from search engine, the accepted answer will work only if the script is in server root directory, as PHP_SELF is filename with path relative to the server root. So the universal solution is

如果有人从搜索引擎到达此处,则只有当脚本位于服务器根目录中时,接受的答案才会起作用,因为PHP_SELF是具有相对于服务器根目录的路径的文件名。所以通用解决方案是

basename($_SERVER['PHP_SELF'])

Also keep in mind, that this returns the top script, for example if you have a script and include a file, and then in included file include another file and try this, you will get the name of the first script, not the second.

另外请记住,这会返回*脚本,例如,如果您有脚本并包含文件,然后在包含文件中包含另一个文件并尝试此操作,您将获得第一个脚本的名称,而不是第二个脚本。

#4


2  

In the code including list.php, before you include, you can set a variable called $this_page and then list.php can see the test for the value of $this_page and act accordingly.

在包含list.php的代码中,在包含之前,可以设置一个名为$ this_page的变量,然后list.php可以看到$ this_page的值的测试并相应地执行。

#5


2  

Perhaps you can do something like the following:

也许你可以做类似以下的事情:

<ul>
    <?php
        $page_name = 'index';
        include("list.php")
    ?>
</ul>

list.php

<?php
    if ($pagename == 'index') {
        //Do something
    }
    else {
        //Do something
    }
?>

#6


1  

The solution basename($_SERVER['PHP_SELF']) works but I recommend to put a strtolower(basename($_SERVER['PHP_SELF'])) to check 'Index.php' or 'index.php' mistakes.

该解决方案基本名($ _ SERVER [ 'PHP_SELF'])的作品,但我建议把一个用strtolower(基名($ _ SERVER [ 'PHP_SELF']))来检查 '的index.php' 或 '的index.php' 的错误。

But if you want an alternative you can do:
<?php if (strtolower(basename($_SERVER['SCRIPT_FILENAME'], '.php')) === 'index'): ?>.

但是如果你想要一个替代方案,你可以这样做:<?php if(strtolower(basename($ _ SERVER ['SCRIPT_FILENAME'],'。php'))==='index'):?>。