Home  >  Article  >  php教程  >  php+shell detect file type

php+shell detect file type

大家讲道理
大家讲道理Original
2016-11-08 14:11:391125browse

Through judgment, the formats of uploaded files are restricted to PDF, docx, xlsx, pptx, potx, vsdx, odt, doc, xls, ppt, vsd, pot, wps, dps, e t and txt, rtf file types. I hope you can give me more opinions!

function checkFileType($filename){
    //文件头
    $_typecode = array(
            '3780',//PDF
            '8075',//.docx,.xlsx,.pptx,.potx,.vsdx,.odt
            '208207',//.doc,.xls,.ppt,.vsd,.pot,.wps,.dps,.et
       );
        $file = fopen($filename, "rb");
        //contents = stream_get_contents($file);
        //$contents = fread($file, filesize($filename));
        $bin = fread($file, 2); //只读2字节
        fclose($file);
        $strInfo = @unpack("C2chars", $bin);// C为无符号整数,网上搜到的都是c,为有符号整数,这样会产生负数判断不正常
        $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
        exec("file $filename",$output,$return_var);//用linux系统命令file判断上传文件的类型,主要是判断txt,rtf文件类型
        $pattern = '/text,/';//rtf和txt文档用file检测都会被检测为text
        $_count =  preg_match($pattern,strrchr($output[0],":"));
        echo $typeCode;
        if(in_array($typeCode,$_typecode) || $_count == 1) {
            return true;
        }else{
            return false;
        }
    }


Statement:
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