Home  >  Article  >  Backend Development  >  PHP获取文件后缀名的几种常用方法总结

PHP获取文件后缀名的几种常用方法总结

WBOY
WBOYOriginal
2016-06-20 13:01:152012browse

使用PHP获取文件后缀名的几种常用方法,通过三种不同的方法

获取文件后缀名,分别通过字符串、文件属性及数组等方式实现。

substr()字符串函数实现的方法:

function get_file_type($filename){
	$type = substr($filename, strrpos($filename, ".")+1);
	return $type;
}

使用pathinfo()函数获取文件后缀名:

function get_file_type($filename){
	$type = pathinfo($filename);
	$type = strtolower($type["extension"]);
	return $type;
}

explode()字符串操作函数获取后缀名:

function get_file_type($filename){  
	$type = explode("." , $filename);
	return end($type);
}

实际开发中,建议使用三种其中更熟悉的一种。


Statement:
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