php下获取当前访问的文件名的几种方法。
推荐函数:
一是PHP获取当前页面的网址: dedecms用的也是这个哦。
<?php //获得当前的脚本网址 function GetCurUrl() { if(!empty($_SERVER["REQUEST_URI"])) { $scriptName = $_SERVER["REQUEST_URI"]; $nowurl = $scriptName; } else { $scriptName = $_SERVER["PHP_SELF"]; if(empty($_SERVER["QUERY_STRING"])) { $nowurl = $scriptName; } else { $nowurl = $scriptName."?".$_SERVER["QUERY_STRING"]; } } return $nowurl; } ?>
下面是详细的例子。
<?php /** * 取得当前访问url的文件名 * edit by www.jbxue.com */ //method 1 $url=$HTTP_SERVER_VARS['REQUEST_URI']; echo(str_replace('/','',$url)); //method 2 $url = $_SERVER['PHP_SELF']; $filename= substr( $url , strrpos($url , ‘/')+1 ); echo $filename; //method 3 $url = $_SERVER['PHP_SELF']; $arr = explode( '/' , $url ); $filename= $arr[count($arr)-1]; echo $filename; //method4 $url = $_SERVER['PHP_SELF']; $filename = end(explode('/',$url)); echo $filename; ?>