当前位置:Gxlcms > PHP教程 > php如何获取文件后缀名

php如何获取文件后缀名

时间:2021-07-01 10:21:17 帮助过:2人阅读

php 怎么获取文件后缀名

很久没写blog了,人越来越懒了,这是做技术的大忌,凡事贵在一个恒“字”,希望以后能继续坚持blog。这次写个获取文件后缀名的php函数来练手吧!

1、采取截字符串的方式

	function getFileExt($file){
		try{
			if(empty($file))return false;	
			$pos=strrpos($file, ".");
			if(!$pos) return false;
			return substr($file, $pos+1);
		
		}catch(Exception $e){
			//
		}
		return false;
	}

2、采用正则分隔

	function getFileExt($file){
		try{
			if(empty($file))return false;
			if($arr=preg_split("/\./",$file)){				
				return array_pop($arr);
			}
		}catch(Exception $e){
			//
		}
		return false;
	}

有没有其他更好的办法呢?


1楼Love__Coder4天前 18:40
怎么是两个empty ?

人气教程排行