php写二进制文件的代码

发布时间:2020-08-30编辑:脚本学堂
收集了一段 php写二进制文件的代码,供大家学习参考。

 收集了一段 php写二进制文件的代码,供大家学习参考。

复制代码 代码如下:
<?php
    /*
        二进制文件格式如下
        +-----------------------------------------------------+
        |总记录数(4B)|空白(7B)--------------------------------|
        |开始ip(4B) |结束ip(4B) |省id(1B)|市id(1B)|ispid(1B)|
        |开始ip(4B) |结束ip(4B) |省id(1B)|市id(1B)|ispid(1B)|
        |开始ip(4B) |结束ip(4B) |省id(1B)|市id(1B)|ispid(1B)|
        +-----------------------------------------------------+
    */
    //要写如的文件

 

    $filename="a.dat";
    //wb表示用二进制重新写文件

    $fp=fopen($filename,'wb')or die("cannot open a.dat");
    //原始数据:开始ip,结束ip,省id,市id,isp的id

    $records=array(
                      array(
                              '110.25.23.1', //开始ip

                              '110.25.23.254', //结束ip

                              3, //省id

                              1, //市id

                              6 //isp的id

                           ),
                      array(
                              '210.200.10.1',
                              '210.254.23.254',
                              1,
                              1,
                              2
                           ),
                     array(
                              '211.68.154.1',
                              '211.254.47.254',
                              2,
                              2,
                              2
                           ),

                                        array(
                              '222.22.45.1',
                              '254.255.255.255',
                              3,
                              3,
                              3
                           ),
                      array(
                              '255.255.255.255',
                              '255.255.255.255',
                              0,
                              0,
                              0
                           )
                  );

    //计算记录总数

    $total=count($records);
    //用4个字节记录记录总数

    $inputstr=sprintf("%08x",$total);
    echo "$inputstr";
    //用4个字节记录记录总数,写入文件

    $str=pack ("H8",$inputstr);
    fwrite($fp,$str);

     //补充7个字节的空白

    $str=pack ("a7","");
    fwrite($fp,$str);
    foreach($records as $item)
    {
        //用4个字节记录开始ip

        $inputstr=sprintf("%x",ip2long($item[0]));
        $str=pack ("H8",$inputstr);
        fwrite($fp,$str);
       
            //用4个字节记录结束ip

        $inputstr=sprintf("%x",ip2long($item[1]));
        $str=pack ("H8",$inputstr);
        fwrite($fp,$str);
       
        //用1个字节记录省id

        $inputstr=sprintf("%02x",$item[2]);
        $str=pack ("H2",$inputstr);
        fwrite($fp,$str);
       
        //用1个字节记录市id

        $inputstr=sprintf("%02x",$item[3]);
        $str=pack ("H2",$inputstr);
        fwrite($fp,$str);
       
            //用1个字节记录ispid

        $inputstr=sprintf("%02x",$item[4]);
        $str=pack ("H2",$inputstr);
        fwrite($fp,$str);
    }
    fclose($fp);
    ?>