php实现不通过扩展名准确判断文件类型的方法【finfo_file方法与二进制流】
2017-04-18 09:47
796 查看
本文实例讲述了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程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- PHP使用finfo_file()函数检测上传图片类型的实现方法
- PHP实现通过中文字符比率来判断垃圾评论的方法
- PHP下通过file_get_contents\curl的方法实现获取远程网页内容(别忘了还有PhpRPC)
- PHP实现通过中文字符比率来判断垃圾评论的方法
- 判断文件类型(文件的真正类型,不是根据扩展名判断),通过文件头来判断
- 网络判断网络连接有多种办法,通过C#程序也可以判断与远程主机的连接状态。具体实现方法
- 通过table标签,PHP输出EXCEL的实现方法
- 一种通过GPS判断手机漫游状态实现自动搜索连接服务商的方法
- PHP中zlib扩展实现GZIP压缩输出各种方法总结
- File Header文件头,通过检查文件头来判断文件类型
- php 读取文件头判断文件类型的实现代码
- 编写js扩展方法实现判断一个数组中是否包含某个元素
- PHP、html通过ajax实现json的方法
- php代码实现读取文件头判断文件类型
- PHP取二进制文件头快速判断文件类型的实现代码
- PHP - Manual手册 - XL. Filesystem 文件系统函数 - is_uploaded_file判断文件是否是通过 HTTP POST 上传的
- PHP下通过file_get_contents的代理使用方法
- 在PHP中通过POST方法实现文件上传功能
- 【PHP扩展开发必备】在PHP扩展中通过C语言调用PHP函数与方法
- 通过RandomAccessFile实现MyRandomAccessFile.readLine()方法--消除乱码