获取文件名(包含扩展):
1.用PHP 文件函数 basename获取
例:
$filename = "/home/httpd/html/index.php";
$file = basename($filename);
2.先获取位置再获取文件名
例:
$filename = "/home/httpd/html/index.php";
$pos = strrpos($filename, '/');
if ($pos === false)
{ // dot is not found in the filename
$file = $filename;
}
else
{
$basename = substr($filename, ($pos+1));
$file = $basename;
}
3.用文件路径分隔符
例:
$filename = "/home/httpd/html/index.php";
$file= end( explode( "/", $filename ) );
4.用pathinfo
例:
$filename = "/home/httpd/html/index.php";
$path_parts = pathinfo($filename);
$file = $path_parts[‘basename’]
5.用正则匹配
例:
$filename = "/home/httpd/html/index.php";
preg_match('/\/([^\/]+\..*)$/', $filename, $matches);
if($matches){
$file = $matches[1];
}
6.用数组弹出最后一个元素
$filename = "/home/httpd/html/index.php";
$file = array_pop(explode('/', $filename));