> php教程 > PHP源码 > 본문

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

大家讲道理
풀어 주다: 2016-11-08 14:11:39
원래의
1116명이 탐색했습니다.

업로드되는 파일의 형식은 판단에 따라 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;
        }
    }
로그인 후 복사


원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!