finfo_open函数不支持,通过二进制流获取文件类型

    public function getMime()
    {
        // $finfo = finfo_open(FILEINFO_MIME_TYPE);
        // return finfo_file($finfo, $this->filename);

        $fp=fopen($this->filename,'r');///实际是image/png
        $bin = fread($fp, 2); //只读2字节
        fclose($fp);
        $str_info = @unpack("C2chars", $bin);//Unpack data from binary string
        $type_code = intval($str_info['chars1'].$str_info['chars2']);// Get the integer value of a variable
        $file_type = "";
        switch ($type_code) {
            case 7790:
             return   $file_type = 'exe';
            break;
            case 7784:
            return  $file_type = 'midi';
            break;
            case 8075:
            return    $file_type = 'zip';
            break;
            case 8297:
            return  $file_type = 'rar';
            break;
            case 255216:
            return  $file_type = 'jpg';
            break;
            case 7173:
            return  $file_type = 'gif';
            break;
            case 6677:
            return   $file_type = 'bmp';
            break;
            case 13780:
            return  $file_type = 'png';
            break;
            default:
            return  $file_type = 'unknown';
            break;
        }

    }

 

posted @ 2017-12-26 19:04  za_szybko  阅读(1211)  评论(0编辑  收藏  举报