Home > Backend Development > PHP Tutorial > php 如何获取文件后缀名

php 如何获取文件后缀名

WBOY
Release: 2016-06-13 13:05:12
Original
760 people have browsed it

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;
	}
Copy after login

             2、采用正则分隔

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

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


1楼Love__Coder4天前 18:40
怎么是两个empty ?
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template