获取当前脚本文件名

时间:2021-09-24 11:52:59

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”部分?

14 个解决方案

#1


331  

Just use the PHP magic constant __FILE__ to get the current filename.

只需使用PHP magic常量__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, '.'));
}

#2


101  

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

当你想要你的include知道它在哪个文件时。实际请求的脚本名称),使用:

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.

编辑:正如亚历克·蒂尔所说,如果你使用符号链接,它会显示符号链接的名称。

#3


55  

see http://php.net/manual/en/function.pathinfo.php

参见http://php.net/manual/en/function.pathinfo.php

pathinfo(__FILE__, PATHINFO_FILENAME);

#4


51  

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.php and current page is index.php, it will return header not index.

basename(__FILE__,“.php”)显示包含此代码的文件的名称——这意味着如果您将此代码包含在header中。php和当前页是索引。php,它会返回header而不是index。

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

basename($_SERVER["REQUEST_URI"]、".php") -如果您使用的是在header中包含此代码。php和当前页是索引。php,它将返回索引而不是header。

#5


23  

This might help:

这可能帮助:

basename($_SERVER['PHP_SELF'])

it will work even if you are using include.

即使您正在使用include,它也会工作。

#6


18  

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

alex的回答是正确的,但是你也可以不使用正则表达式这样做:

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

#7


11  

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 >

#8


6  

you can also use this:

你也可以用这个:

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

#9


4  

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.

也会做你需要的。

#10


1  

Try This

试试这个

$current_file_name = $_SERVER['PHP_SELF'];
echo $current_file_name;

#11


0  

$filename = "jquery.js.php";
$ext = pathinfo($filename, PATHINFO_EXTENSION);//will output: php
$file_basename = pathinfo($filename, PATHINFO_FILENAME);//will output: jquery.js

#12


0  

__FILE__ use examples based on localhost server results:

__FILE__使用基于本地主机服务器结果的示例:

echo __FILE__;
// C:\LocalServer\www\templates\page.php

echo strrchr( __FILE__ , '\\' );
// \page.php

echo substr( strrchr( __FILE__ , '\\' ), 1);
// page.php

echo basename(__FILE__, '.php');
// page

#13


0  

$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 $argv array. 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

    回声__FILE__;返回类似的/我的/目录/路径/ my_script.php

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

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

#14


0  

As some said basename($_SERVER["SCRIPT_FILENAME"], '.php') and basename( __FILE__, '.php') are good ways to test this.

正如一些人所说的basename($_SERVER["SCRIPT_FILENAME"], '.php')和basename(__FILE__, '.php')是测试这个的好方法。

To me using the second was the solution for some validation instructions I was making

对我来说,使用第二个是我所做的一些验证指令的解决方案

#1


331  

Just use the PHP magic constant __FILE__ to get the current filename.

只需使用PHP magic常量__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, '.'));
}

#2


101  

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

当你想要你的include知道它在哪个文件时。实际请求的脚本名称),使用:

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.

编辑:正如亚历克·蒂尔所说,如果你使用符号链接,它会显示符号链接的名称。

#3


55  

see http://php.net/manual/en/function.pathinfo.php

参见http://php.net/manual/en/function.pathinfo.php

pathinfo(__FILE__, PATHINFO_FILENAME);

#4


51  

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.php and current page is index.php, it will return header not index.

basename(__FILE__,“.php”)显示包含此代码的文件的名称——这意味着如果您将此代码包含在header中。php和当前页是索引。php,它会返回header而不是index。

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

basename($_SERVER["REQUEST_URI"]、".php") -如果您使用的是在header中包含此代码。php和当前页是索引。php,它将返回索引而不是header。

#5


23  

This might help:

这可能帮助:

basename($_SERVER['PHP_SELF'])

it will work even if you are using include.

即使您正在使用include,它也会工作。

#6


18  

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

alex的回答是正确的,但是你也可以不使用正则表达式这样做:

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

#7


11  

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 >

#8


6  

you can also use this:

你也可以用这个:

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

#9


4  

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.

也会做你需要的。

#10


1  

Try This

试试这个

$current_file_name = $_SERVER['PHP_SELF'];
echo $current_file_name;

#11


0  

$filename = "jquery.js.php";
$ext = pathinfo($filename, PATHINFO_EXTENSION);//will output: php
$file_basename = pathinfo($filename, PATHINFO_FILENAME);//will output: jquery.js

#12


0  

__FILE__ use examples based on localhost server results:

__FILE__使用基于本地主机服务器结果的示例:

echo __FILE__;
// C:\LocalServer\www\templates\page.php

echo strrchr( __FILE__ , '\\' );
// \page.php

echo substr( strrchr( __FILE__ , '\\' ), 1);
// page.php

echo basename(__FILE__, '.php');
// page

#13


0  

$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 $argv array. 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

    回声__FILE__;返回类似的/我的/目录/路径/ my_script.php

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

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

#14


0  

As some said basename($_SERVER["SCRIPT_FILENAME"], '.php') and basename( __FILE__, '.php') are good ways to test this.

正如一些人所说的basename($_SERVER["SCRIPT_FILENAME"], '.php')和basename(__FILE__, '.php')是测试这个的好方法。

To me using the second was the solution for some validation instructions I was making

对我来说,使用第二个是我所做的一些验证指令的解决方案