Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error when output binary data with PHP

Tags:

php

I write a simple php to load some binary data from DB then output to client.

    $sql="select FightPlayEnd from ZMTXLogic.FightLog where ID=".addslashes($id);
    $result=$db->query($sql);

    if($db->num_rows($result)>0)
    {

        $row = mysql_fetch_assoc($result);
        $nByteCount = mb_strlen($row["FightPlayEnd"], '8bit');
        //echo $nByteCount;

        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Accept-Length:".$nByteCount);
        header("Content-Disposition:attachment;filename=FightPlayEnd.bin");
        header( "Content-type: application/octet-stream");

        echo $row["FightPlayEnd"];
    }

The problem is the data got from IE is not same as original binary data but added EF BB BF(view in UltraEdit) at the header and 0D 0A 0D 0A at the end. What is wrong with it?

like image 291
Spark Avatar asked Jan 20 '26 11:01

Spark


1 Answers

0D 0A 0D 0A is just \r\n\r\n, that is, two linebreaks.

EE BB BF is a byte order mark. It signals UTF-8 encoding.

Edit (see comments):

Your script might be outputting more than it should (particularly those \r\n\r\n).

You need to clean the output buffer before you start outputting the data (ob_clean()) and exit right after your echo.

like image 176
Dennis Avatar answered Jan 23 '26 00:01

Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!