Get the current script file name

2020-01-23 04:16发布

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

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

标签: php file
15条回答
▲ chillily
2楼-- · 2020-01-23 04:34

__FILE__ use examples based on localhost server results:

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
查看更多
成全新的幸福
3楼-- · 2020-01-23 04:36

Here is the difference between basename(__FILE__, ".php") and 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($_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.

查看更多
别忘想泡老子
4楼-- · 2020-01-23 04:39

you can also use this:

echo $pageName = basename($_SERVER['SCRIPT_NAME']);
查看更多
Emotional °昔
5楼-- · 2020-01-23 04:40

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

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

查看更多
该账号已被封号
6楼-- · 2020-01-23 04:41

This might help:

basename($_SERVER['PHP_SELF'])

it will work even if you are using include.

查看更多
Summer. ? 凉城
7楼-- · 2020-01-23 04:42

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

pathinfo(__FILE__, PATHINFO_FILENAME);
查看更多
登录 后发表回答