>  기사  >  php教程  >  php+shell은 파일 유형을 감지합니다

php+shell은 파일 유형을 감지합니다

大家讲道理
大家讲道理원래의
2016-11-08 14:11:391093검색

업로드되는 파일의 형식은 판단에 따라 PDF, docx, xlsx, pptx, potx, vsdx, odt, doc, xls, ppt, vsd, pot, wps, dps, e t 및 txt로 제한됩니다. 더 많은 rtf 파일 형식!

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;
        }
    }


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.