Home > php教程 > php手册 > PHP 获取文件的扩展名的6种方法

PHP 获取文件的扩展名的6种方法

WBOY
Release: 2016-06-13 10:19:01
Original
1085 people have browsed it

昨天, 和一个朋友在PHP交流群(276167802, 验证:csl,有兴趣的话可以加入进来一起讨论)里提到PHP 获取文件的扩展名的6种方法,现在拿出来和大家分享一下:

1、字符串查找和截取的方法


$extension=substr(strrchr($file, '.'), 1);
Copy after login


2、字符串查找和截取的方法二


$extension=substr($file, strrpos($file, '.')+1);
Copy after login


3、数组分割的方法


$extension=end(explode('.', $file));
Copy after login


4、使用pathinfo直接解析的方法


$info = pathinfo($file);
$extension=$info['extension'];
Copy after login


5、使用pathinfo的第二个参数


$extension=pathinfo($file, PATHINFO_EXTENSION);
Copy after login


6、使用finfo_file函数


$finfo = finfo_open(FILEINFO_MIME_TYPE);
$extension = finfo_file($finfo, $file) ;
echo $extension;
finfo_close($finfo);
Copy after login


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template