在PHP中获取文件名

在PHP中获取文件名,php,Php,如何在PHP中获取文件名?我想做的是让一个函数获取一个文件名字符串,然后做一些事情,如果它真的在页面上 function onThisPageFunction(fileNameFromFunction) { if(onThisPage == fileNameFromFunction) { do stuff } } 如果我理解正确,您需要\uuuuuuuuuuuuuuuuuu文件 Ref:有两个变量将为您提供文件名 例如:调用/test/test1.p

如何在PHP中获取文件名?我想做的是让一个函数获取一个文件名字符串,然后做一些事情,如果它真的在页面上

function onThisPageFunction(fileNameFromFunction)  
{  
  if(onThisPage == fileNameFromFunction)  
  {  
    do stuff  
  }  
}

如果我理解正确,您需要
\uuuuuuuuuuuuuuuuuu文件


Ref:

有两个变量将为您提供文件名

例如:调用/test/test1.php作为url,test1.php包括test2.php

将为您提供执行代码的当前文件,在本例中,它将返回“test2.php”

将为您提供原始调用的完整url,在本例中为“/test/test1.php”


请注意,您可以使用该函数仅从路径获取文件名。

我认为您需要使用:

_SERVER["PHP_SELF"]
例如:

意志


也可以在命令行中使用。

感谢您的回复。这就是最终为我做这项工作的原因

function menuStrikethrough($pageName)
    {
        // get the name of the page we're on
        $fileName = basename($_SERVER['PHP_SELF']);

        // if fileName equals pageName then output css to show a strikethrough
        if($fileName == $pageName)
        {
            echo ' style="text-decoration:line-through" ';
        }
    }
我发现
\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu

basename提示派上了用场

谢谢你的帮助

if (realpath($_SERVER['SCRIPT_NAME']) === __FILE__) {
    echo "Called directly";
}
function menuStrikethrough($pageName)
    {
        // get the name of the page we're on
        $fileName = basename($_SERVER['PHP_SELF']);

        // if fileName equals pageName then output css to show a strikethrough
        if($fileName == $pageName)
        {
            echo ' style="text-decoration:line-through" ';
        }
    }