PHP流判断上传文件是否合法

 $fp = fopen($file['tmp_name'], "rb");
        $bin = fread($fp, 2); //只读2字节
        fclose($fp);
        $str_info  = @unpack("C2chars", $bin);
        $type_code = intval($str_info['chars1'].$str_info['chars2']);
        switch ($type_code) {
            case 255216:
                $file_type = 'jpg';
                break;
            case 7173:
                $file_type = 'gif';
                break;
            case 13780:
                $file_type = 'png';
                break;
            default:
                $file_type = 'unknown';
                break;
        }

        $typeAllow = array('jpg','gif','png');

        if( ! in_array($file_type, $typeAllow) ) {
            $data['status'] = 0;
            $data['info'] = '图像文件不合法'.$file_type;
            return $data;
        }

你可能感兴趣的:(PHP相关)