页面载入中,请稍候...

PHP通过读取文件头部两个字节判断文件真实类型及其应用

2012-01-29 14:25:04    作者:admpub    来源:会员投稿    评论:读取中    点击:读取中    [我要投稿]

通过判断文件头部的前两个字节就能判断出文件的真实类型,下面有两个函数:function checkFileType($fileName){ $file = fopen($fileNa...

通过判断文件头部的前两个字节就能判断出文件的真实类型,下面有两个函数:

\

function checkFileType($fileName){

        $file     = fopen($fileName, "rb");
        $bin      = fread($file, 2); //只读2字节
        fclose($file);
        $strInfo  = @unpack("C2chars", $bin);// C为无符号整数,网上搜到的都是c,为有符号整数,这样会产生负数判断不正常
        $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
        $fileType = '';

        if($typeCode == 255216 /*jpg*/ || $typeCode == 7173 /*gif*/ || $typeCode == 13780 /*png*/) {
            return true;
        }else{
            return false;
        }

}
   function file_type($filename){
     $file = fopen($filename, "rb");
     $bin = fread($file, 2); //只读2字节
     fclose($file);
     $strInfo = @unpack("C2chars", $bin);
     $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
     $fileType = '';
     switch ($typeCode)
     {
         case 7790:
             $fileType = 'exe';
             break;
         case 7784:
             $fileType = 'midi';
             break;
         case 8297:
             $fileType = 'rar';
             break;
         case 255216:
             $fileType = 'jpg';
             break;
         case 7173:
             $fileType = 'gif';
             break;
         case 6677:
             $fileType = 'bmp';
             break;
         case 13780:
             $fileType = 'png';
             break;
         default:
             $fileType = 'unknown';
     }
     return $fileType;
 }

示例,判断真实类型的php上传类:

/**
 * 读取文件头部判断文件准确类型
 */
class UpLoader{

    private $path;
    private $files;
    private $checkFunction;

    /**
     * @param $allow_type 接受参数为过滤类型,目前有"images"和"zip"两种,可以扩展
     */
     function UpLoader($allow_type){

            $this->path = getenv('SINASRV_CACHE_DIR').'zhuanqu_files/';

            switch($allow_type){
                case 'image':
                    $this->checkFunction = 'checkIfImage';
                break;
                case 'zip':
                    $this->checkFunction = 'checkIfZip';
                break;
            }

     }

    /**
     * 执行上传
     * @param $files 参数为$_FILES数组
     */
     public function doUpload($files){

            $this->files = $files;
            $func = $this->checkFunction;

             if($this->files['files']['name'] != ''  && $this->files['files']['error'] == UPLOAD_ERR_OK) {

                $tmp_name = $this->files["files"]["tmp_name"];

                //检查文件类型
                if( $this->$func($tmp_name) ){
                    $name = md5_file($tmp_name);
                    $name = $name.'___'.$this->files["files"]["name"];
                    if(move_uploaded_file($tmp_name, $this->path.$name)){
                        //echo ' "上传成功!" ';
                    }
                }
             }
     }

     private function checkIfImage($fileName){

        $file     = fopen($fileName, "rb");
        $bin      = fread($file, 2); //只读2字节
        fclose($file);
        $strInfo  = @unpack("C2chars", $bin);
        $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
        $fileType = '';

        if($typeCode == 255216 /*jpg*/ || $typeCode == 7173 /*gif*/ || $typeCode == 13780 /*png*/) {
            return true;
        }else{
           // echo '"仅允许上传jpg/gif/png格式的图片!';
            return false;
        }
     }

     private function checkIfZip($fileName){

        $file     = fopen($fileName, "rb");
        $bin      = fread($file, 2); //只读2字节
        fclose($file);
        $strInfo  = @unpack("C2chars", $bin);
        $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
        $fileType = '';

        if($typeCode == 8075) {
            return true;
        }else{
            //echo '"仅允许上传zip格式的文件!"';
            return false;
        }
    }

 }

相关热词搜索:判断文件类型  

上一篇:PHP汉字转换成拼音的方法(支持UTF-8和g2312)
下一篇:php xss 过滤函数,过滤内容比较全面

分享到: 收藏