php 获取当前脚本文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4221333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:19:31  来源:igfitidea点击:

Get the current script file name

phpfile

提问by Alex

If I have PHP script, how can I get the filename from inside that script?

如果我有 PHP 脚本,如何从该脚本中获取文件名?

Also, given the name of a script of the form jquery.js.php, how can I extract just the "jquery.js" part?

另外,给定表单脚本的名称jquery.js.php,如何仅提取“jquery.js”部分?

回答by alex

Just use the PHP magic constant__FILE__to get the current filename.

只需使用PHP 魔术常量__FILE__即可获取当前文件名。

But it seems you want the part without .php. So...

但似乎你想要没有.php. 所以...

basename(__FILE__, '.php'); 

A more generic file extension remover would look like this...

一个更通用的文件扩展名移除器看起来像这样......

function chopExtension($filename) {
    return pathinfo($filename, PATHINFO_FILENAME);
}

var_dump(chopExtension('bob.php')); // string(3) "bob"
var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"

Using standard string library functions is much quicker, as you'd expect.

正如您所期望的那样,使用标准字符串库函数要快得多

function chopExtension($filename) {
    return substr($filename, 0, strrpos($filename, '.'));
}

回答by SparK

When you want your include to know what file it is in (ie. what script name was actually requested), use:

当您希望包含知道它在哪个文件中(即实际请求的脚本名称)时,请使用:

basename($_SERVER["SCRIPT_FILENAME"], '.php')

Because when you are writing to a file you usually know its name.

因为当您写入文件时,您通常知道它的名称。

Edit: As noted by Alec Teal, if you use symlinks it will show the symlink name instead.

编辑:正如 Alec Teal 所指出的,如果您使用符号链接,它将显示符号链接名称。

回答by max4ever

回答by Khandad Niazi

Here is the difference between basename(__FILE__, ".php")and basename($_SERVER['REQUEST_URI'], ".php").

这里的区别basename(__FILE__, ".php")basename($_SERVER['REQUEST_URI'], ".php")

basename(__FILE__, ".php")shows the name of the file where this code is included - It means that if you include this code in header.phpand current page is index.php, it will return headernot index.

basename(__FILE__, ".php")显示包含此代码的文件的名称 - 这意味着如果您在header.php 中包含此代码并且当前页面是index.php,它将返回header而不是index

basename($_SERVER["REQUEST_URI"], ".php")- If you use include this code in header.phpand current page is index.php, it will return indexnot header.

basename($_SERVER["REQUEST_URI"], ".php")- 如果您在header.php 中使用包含此代码 并且当前页面是index.php,它将返回index而不是header

回答by charan315

This might help:

这可能有帮助:

basename($_SERVER['PHP_SELF'])

it will work even if you are using include.

即使您正在使用包含,它也会起作用。

回答by user

alex's answer is correct but you could also do this without regular expressions like so:

亚历克斯的答案是正确的,但您也可以在没有正则表达式的情况下执行此操作,如下所示:

str_replace(".php", "", basename($_SERVER["SCRIPT_NAME"]));

回答by begoyan

Here is a list what I've found recently searching an answer:

这是我最近在搜索答案时发现的列表:

//self name with file extension
echo basename(__FILE__) . '<br>';
//self name without file extension
echo basename(__FILE__, '.php') . '<br>';
//self full url with file extension
echo __FILE__ . '<br>';

//parent file parent folder name
echo basename($_SERVER["REQUEST_URI"]) . '<br>';
//parent file parent folder name with //s
echo $_SERVER["REQUEST_URI"] . '<br>';

// parent file name without file extension
echo basename($_SERVER['PHP_SELF'], ".php") . '<br>';
// parent file name with file extension
echo basename($_SERVER['PHP_SELF']) . '<br>';
// parent file relative url with file etension
echo $_SERVER['PHP_SELF'] . '<br>';

// parent file name without file extension
echo basename($_SERVER["SCRIPT_FILENAME"], '.php') . '<br>';
// parent file name with file extension
echo basename($_SERVER["SCRIPT_FILENAME"]) . '<br>';
// parent file full url with file extension
echo $_SERVER["SCRIPT_FILENAME"] . '<br>';

//self name without file extension
echo pathinfo(__FILE__, PATHINFO_FILENAME) . '<br>';
//self file extension
echo pathinfo(__FILE__, PATHINFO_EXTENSION) . '<br>';

// parent file name with file extension
echo basename($_SERVER['SCRIPT_NAME']);

Don't forget to remove :)

不要忘记删除:)

<br>

<br>

回答by Shah Alom

you can also use this:

你也可以使用这个:

echo $pageName = basename($_SERVER['SCRIPT_NAME']);

回答by Megachip

A more general way would be using pathinfo(). Since Version 5.2 it supports PATHINFO_FILENAME.

更通用的方法是使用pathinfo()。从 5.2 版开始,它支持PATHINFO_FILENAME.

So

所以

pathinfo(__FILE__,PATHINFO_FILENAME)

will also do what you need.

也会做你需要的。

回答by JBH

$argv[0]

$argv[0]

I've found it much simpler to use $argv[0]. The name of the executing script is always the first element in the $argvarray. Unlike all other methods suggested in other answers, this method does not require the use of basename()to remove the directory tree. For example:

我发现使用起来要简单得多$argv[0]。执行脚本的名称始终是$argv数组中的第一个元素。与其他答案中建议的所有其他方法不同,此方法不需要使用basename()来删除目录树。例如:

  • echo __FILE__;returns something like /my/directory/path/my_script.php

  • echo $argv[0];returns my_script.php

  • echo __FILE__;返回类似的东西 /my/directory/path/my_script.php

  • echo $argv[0];返回 my_script.php