• 技术文章 >后端开发 >php教程

    php+shell检测文件类型

    2016-07-25 09:01:36原创569
    通过判断 限制上传文件的格式为 PDF,docx,xlsx,pptx,potx,vsdx,odt,doc,xls,ppt,vsd,pot,wps,dps,et和txt,rtf文件类型

    希望大家多提意见!
    1. function checkFileType($filename){
    2. //文件头
    3. $_typecode = array(
    4. '3780',//PDF
    5. '8075',//.docx,.xlsx,.pptx,.potx,.vsdx,.odt
    6. '208207',//.doc,.xls,.ppt,.vsd,.pot,.wps,.dps,.et
    7. );
    8. $file = fopen($filename, "rb");
    9. //contents = stream_get_contents($file);
    10. //$contents = fread($file, filesize($filename));
    11. $bin = fread($file, 2); //只读2字节
    12. fclose($file);
    13. $strInfo = @unpack("C2chars", $bin);// C为无符号整数,网上搜到的都是c,为有符号整数,这样会产生负数判断不正常
    14. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
    15. exec("file $filename",$output,$return_var);//用linux系统命令file判断上传文件的类型,主要是判断txt,rtf文件类型
    16. $pattern = '/text,/';//rtf和txt文档用file检测都会被检测为text
    17. $_count = preg_match($pattern,strrchr($output[0],":"));
    18. echo $typeCode;
    19. if(in_array($typeCode,$_typecode) || $_count == 1) {
    20. return true;
    21. }else{
    22. return false;
    23. }
    24. }
    复制代码
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:php+shell检测文件类型
    上一篇:约瑟夫环问题(环形链表) 下一篇:仿iPad界面的在线群聊
    20期PHP线上班

    相关文章推荐

    精选22门好课,价值3725元,开通VIP免费学习!• 细思极恐?关于in_array的第3个参数• PHP无限分类的类_php技巧• 超强分页类2.0发布,支持自定义风格,默认4种显示模式_php实例• PHP递归创建多级目录_PHP• php中memcache 基本操作实例_PHP
    1/1

    PHP中文网