如果我有PHP脚本,我怎么能得到当前执行的文件没有其扩展名的文件名?

给定一个“jquery.js.php”形式的脚本的名称,我如何提取只是“jquery.js”部分?


当前回答

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

其他回答

__FILE__使用基于localhost服务器结果的示例:

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

当你想让你的包含知道它在什么文件(即。实际请求的脚本名称),使用:

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

因为当你写入一个文件时,你通常知道它的名字。

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

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

试试这个

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

以下是我最近在寻找答案时发现的一个列表:

//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']);

别忘了删除:)

< br >