php实现不通过扩展名准确判断文件类型的方法【finfo_file方法与二进制流】


Posted in PHP onApril 18, 2017

本文实例讲述了php实现不通过扩展名准确判断文件类型的方法。分享给大家供大家参考,具体如下:

第一种方法

通过php的finfo_file()

$handle=finfo_open(FILEINFO_MIME_TYPE);//This function opens a magic database and returns its resource. 
$fileInfo=finfo_file($handle,'./test.txt');// Return information about a file
finfo_close($handle);
print_r($fileInfo);
echo '==========="\n"';

另外

finfo_buffer: Return information about a string buffer
finfo_close: Close fileinfo resource
mime_content_type:Detect MIME Content-type for a file (deprecated)

第二种方法

通过二进制流获取文件内容

$fp=fopen('test.txt','r')///实际是image/png
$bin = fread($fp, 2); //只读2字节
fclose($fp);
$str_info = @unpack(“C2chars”, $bin);//Unpack data from binary string
$type_code = intval($str_info['chars1'].$str_info['chars2']);// Get the integer value of a variable
$file_type = ”;
switch ($type_code) {
case 7790:
$file_type = 'exe';
break;
case 7784:
$file_type = 'midi';
break;
case 8075:
$file_type = 'zip';
break;
case 8297:
$file_type = 'rar';
break;
case 255216:
$file_type = 'jpg';
break;
case 7173:
$file_type = 'gif';
break;
case 6677:
$file_type = 'bmp';
break;
case 13780:
$file_type = 'png';
break;
default:
$file_type = 'unknown';
break;
}

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
杏林同学录(二)
Oct 09 PHP
php操作mysqli(示例代码)
Oct 28 PHP
php实现执行某一操作时弹出确认、取消对话框
Dec 30 PHP
php实现网页缓存的工具类分享
Jul 14 PHP
PHP基于文件存储实现缓存的方法
Jul 20 PHP
10个超级有用的PHP代码片段果断收藏
Sep 23 PHP
Yii实现复选框批量操作实例代码
Mar 15 PHP
ThinkPHP使用getlist方法实现数据搜索功能示例
May 08 PHP
PHP实现向关联数组指定的Key之前插入元素的方法
Jun 06 PHP
PHP操作MySQL中BLOB字段的方法示例【存储文本与图片】
Sep 15 PHP
TP5框架请求响应参数实例分析
Oct 17 PHP
浅析PHP反序列化中过滤函数使用不当导致的对象注入问题
Feb 15 PHP
基于thinkPHP3.2实现微信接入及查询token值的方法
Apr 18 #PHP
PHP递归删除多维数组中的某个值
Apr 17 #PHP
Thinkphp5.0自动生成模块及目录的方法详解
Apr 17 #PHP
php正则表达式基本知识与应用详解【经典教程】
Apr 17 #PHP
PHP中快速生成随机密码的几种方式
Apr 17 #PHP
IIS 7.5 asp Session超时时间设置方法
Apr 17 #PHP
关于php 高并发解决的一点思路
Apr 16 #PHP
You might like
PHP校验ISBN码的函数代码
2011/01/17 PHP
PHP延迟静态绑定的深入讲解
2018/04/02 PHP
javascript中的变量是传值还是传址的?
2010/04/19 Javascript
jquery 单击li防止重复加载的实现代码
2010/12/24 Javascript
js实现屏蔽默认快捷键调用自定义事件示例
2013/06/18 Javascript
Window.Open打开窗体和if嵌套代码
2016/04/15 Javascript
微信小程序教程之本地图片上传(leancloud)实例详解
2016/11/16 Javascript
微信小程序中使元素占满整个屏幕高度实现方法
2016/12/14 Javascript
详解使用fetch发送post请求时的参数处理
2017/04/05 Javascript
原生JS实现N级菜单的代码
2017/05/21 Javascript
详解vue-cli官方脚手架配置
2018/07/20 Javascript
js面向对象封装级联下拉菜单列表的实现步骤
2021/02/08 Javascript
讲解Python中if语句的嵌套用法
2015/05/14 Python
理解Python垃圾回收机制
2016/02/12 Python
python生成excel的实例代码
2017/11/08 Python
对pandas replace函数的使用方法小结
2018/05/18 Python
ubuntu17.4下为python和python3装上pip的方法
2018/06/12 Python
python 拼接文件路径的方法
2018/10/23 Python
Python DataFrame一列拆成多列以及一行拆成多行
2019/08/06 Python
python如何实现不用装饰器实现登陆器小程序
2019/12/14 Python
python+selenium 脚本实现每天自动登记的思路详解
2020/03/11 Python
Python如何使用paramiko模块连接linux
2020/03/18 Python
Python基于数列实现购物车程序过程详解
2020/06/09 Python
python3.8动态人脸识别的实现示例
2020/09/21 Python
Canvas 文本转粒子效果的实现代码
2019/02/14 HTML / CSS
HTML5新增元素如何兼容旧浏览器有哪些方法
2014/05/09 HTML / CSS
TripAdvisor斯洛伐克:阅读评论、比较价格和酒店预订
2018/04/25 全球购物
你经历的项目中的SCM配置项主要有哪些?什么是配置项?
2013/11/04 面试题
致跳远、跳高运动员广播稿
2014/01/09 职场文书
货物运输服务质量承诺书
2014/05/29 职场文书
师范生求职信
2014/06/14 职场文书
计算机科学与技术专业求职信
2014/09/03 职场文书
幼儿园教师师德师风演讲稿:爱我所爱 无悔青春
2014/09/10 职场文书
音乐教师个人总结
2015/02/06 职场文书
golang goroutine顺序输出方式
2021/04/29 Golang
windows10 家庭版下FTP服务器搭建教程
2022/08/05 Servers