php获取一个文件名的函数,PHP 文件系统函数之获取文件名及文件名后缀-php文件...
獲取文件名(包含擴展):
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));
總結
以上是生活随笔為你收集整理的php获取一个文件名的函数,PHP 文件系统函数之获取文件名及文件名后缀-php文件...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 百度php editor图片上传到其他盘
- 下一篇: linkbox php,win10 do