获取当前脚本文件名 [英] Get the current script file name

查看:113
本文介绍了获取当前脚本文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

另外,给定了一个 jquery.js.php ,怎样才能提取jquery.js部分?

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

推荐答案

只需使用 PHP魔术常数 __FILE __ 以获取当前文件名。

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

但是,似乎你想要的零件没有 .php 。所以...

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

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, '.'));
}

这篇关于获取当前脚本文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆