php实现不通过扩展名准确判断文件类型的方法【finfo_file方法与二进制流】
本文实例讲述了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文件操作总结》、《PHP网络编程技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
相关文章
Windows下安装PHP单元测试环境PHPUnit图文教程
这篇文章主要介绍了Windows下安装PHP单元测试环境PHPUnit图文教程,按照本文操作即可在Windows下安装成功PHPUnit,需要的朋友可以参考下2014-10-10
php5.3 不支持 session_register() 此函数已启用的解决方法
php从5.2.x升级到5.3.2.出来问题了。有些原来能用的程序报错了,Deprecated: Function session_register() is deprecated2013-11-11
How do I change MySQL timezone?
The MySQL timezone is set to MST (-7 hours GMT/UTC) and is not configurable by you. MySQL is only capable of having 1 timezone setting per mysql daemon. Therefore, you cannot select NOW() and expect a result in a timezone other than MST.2008-03-03
php官方微信接口大全(微信支付、微信红包、微信摇一摇、微信小店)
这篇文章主要介绍了php官方微信接口大全,包括微信支付、微信红包、微信摇一摇、微信小店,感兴趣的小伙伴们可以参考一下2015-12-12
php使用gettimeofday函数返回当前时间并存放在关联数组里
这篇文章主要介绍了php使用gettimeofday函数返回当前时间并存放在关联数组里的方法,涉及php中gettimeofday函数的使用技巧,需要的朋友可以参考下2015-03-03


最新评论