php finfo_file() function detects uploaded image type

怪我咯
Release: 2023-03-13 06:54:02
Original
2333 people have browsed it

这篇文章主要介绍了PHP使用finfo_file()函数检测上传图片类型的实现方法,结合实例形式分析了finfo_file()函数的功能、使用方法及相关注意事项,需要的朋友可以参考下

本文实例讲述了PHP使用finfo_file()函数检测上传图片类型的实现方法。分享给大家供大家参考,具体如下:

在输入输出中,文件的交互必不可少,比如文件的上传什么的。这里我们来解决一个小问题,就是如何判断用户上传文件的文件类型。

举一个应用场面:在我们的Web应用中,比如用户上传头像,要求是png,jpg,gif格式,接收到图片后会根据图片格式类型做不同的头像切割处理,但个别用户会传一些只更改过文件后缀的非标准图片,比如nowamagic.jpg 强行修改成 nowamagic.png,这样程序无法对该类型图片进行切割,切割失败。这个时候就需要对文件进行检测,并且仅仅根据后缀来判断是不行的。

这种这么常见的需求,PHP肯定自带了某些函数来实现的,这个函数就是finfo_file

finfo_file

(PHP >= 5.3.0, PECLfileinfo>= 0.1.0)

finfo_file -- finfo::file — Return information about a file,This function is used to get information about a file.

函数很简单,看看官方文档就大概清楚了,这里就举一个使用例子吧,就是上传一张图片,并判断它的类型:

   文件上传 
  


"; } $finfo = finfo_open(FILEINFO_MIME_TYPE); //$mime = finfo_file($finfo, dirname(FILE)."/".$filename); $mime = finfo_file($finfo, $savepath); echo $mime; } ?>
Copy after login

需要注意的是,finfo_file 函数需要 PHP 版本 >= 5.3.0,如果低于 5.3,那么需要到 php.ini 里,将 extension=php_fileinfo.dll 前面的分号去掉来启用

The above is the detailed content of php finfo_file() function detects uploaded image type. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!