How PHP receives binary files how to replace content inside

Source: Internet
Author: User
Tags printable characters sqlite database
PHP receives the binary file as follows:
Header (' content-type:text/html; Charset=utf-8 ');
error_reporting (0);
$filename = $_get["filename"];
$filesize = $_get["FileSize"];

$xmlstr = $GLOBALS [http_raw_post_data];//$_post["DATA"];//
if (empty ($xmlstr)) $xmlstr = file_get_contents (' php://input ');

$raw = $xmlstr;//get post binary raw data
$file = fopen ("./upload/". $filename, "w");//Open file ready to write
Fwrite ($file, $raw);//write
Fclose ($file);//Close
?>

bytes of non-standard characters (>0x7f) in the received binaries are replaced with three bytes, such as E2 replaced with EF 9F A2
Now I want to restore back after PHP received the file, the EF 9F A2 replaced with E2, how this implementation?

Thank you so much!


Reply to discussion (solution)

Didn't you get an XML stream?
It's a text file, not a binary file.
Aren't all the >0x7f utf-8 wide characters?

You better post $raw content.

fopen ("./upload/". $filename, "W B");
Give it a try

Add
Utf-8 EF 9F A2 should be Unicode f7e2 instead of E2
and e000-f8ff belongs to the user-defined character (that is, no character set, the area to be used by the user)
For example, some communication interfaces use custom characters for boundary recognition.

Pay attention to this.

The sending side, which is set up in this format:
if (Xmlhttp.overridemimetype) {
Xmlhttp.overridemimetype (' Text/plain; charset=x-user-defined ');
} else {
Xmlhttp.setrequestheader (' Accept-charset ', ' x-user-defined ');
}
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded;charset=utf-8");

Send files and receive files:

You're transmitting a binary stream.
According to the Convention of XML, it should be transmitted by Base64 encoding.
Otherwise, it would be problematic as a text transfer.

Whether the transfer can be declared as binary data, this need to check the data

Find this method on the Internet:
XMLHttpRequest.prototype.sendAsBinary = function (datastr) {
function Bytevalue (x) {
Return x.charcodeat (0) & 0xFF;
}
var ords = Array.prototype.map.call (Datastr, Bytevalue);
var ui8a = new window. Uint8array (ords);
This.send (Ui8a.buffer);
}
Xmlhttp.sendasbinary (content);

You can use IE browser on your PC.
But I need to use on Android browser, do not support this function uint8array, so do not know how to send with binary stream.

Compare sending and receiving files, or regular:
Add prefixes greater than C0 to EF 9F, such as E2 replaced with EF 9F A2
The prefix of less than C0 is EF 9E, as B2 is replaced by EF 9E B2

This kind of code does not know how to come out? So I want to say that the hexadecimal of the receiving file is replaced, as long as the result is correct.

Is there any way to replace the binary number?
Can regular expressions support it?

Str_replace, STRTR can be, regular can also
Such as
$s = Str_replace ("\xef\x9e\xb2", "\xb2", $s);

But
Add prefixes greater than C0 to EF 9F, such as E2 replaced with EF 9F A2
What are the rules?

The rules for his data conversion are:
File header unchanged, the length of the file is not clear, the figure on the 0050h are the same
<=7f byte, because Utf-8 is also the same, can be considered also converted Utf-8
>=80 byte, High plus F7, then utf-8, for example E2 becomes f7e2 then turn utf-8 into EF 9F A2

At least he e2/fb/91/b2/81 in the picture ... all the rules.

He pictured the SQLite database file, obviously what kind of data in the database can be stored, byte value 0x00-0xff
If it proves to be as #10 said, then write a function.
Algorithm testing

$s =<<< TXT00 2D ef 8F A2 1 a The EF 9F bb 0000-XX 0E-EF 9F BB-6B-EF-9E-----3600 EF 9E B2 the EF 9E ba-xx ef 9E bayi xx 2FTXT; $s = preg_replace ("/[\r\n]/", ", $s); $s = pack (' h* ', $s); Constructs the binary data $m = 0;for ($i =0; $i
 
  2D E2 1 A (xx), FB, XX, 0E
  
6B B2 01 60 00 81 00 81
XX 2F

Str_replace, STRTR can be, regular can also
Such as
$s = Str_replace ("\xef\x9e\xb2", "\xb2", $s);

But
Add prefixes greater than C0 to EF 9F, such as E2 replaced with EF 9F A2
What are the rules?

The C0 prefix is EF 9F, and the EF 9F A2 is replaced with a2+40, how is this regular expression written?

Use the regular to write like this
Assuming that the data has been $s in the existing variable, the

$s = Preg_replace_callback ('/[\xef]. /', ' foo ', $s), function foo ($r) {  $c = (ord ($r [0]{1}) & 0x03) << 6;  $c + = (ord ($r [0]{2}) & 0x3f);  return Chr ($c);}

He pictured the SQLite database file, obviously what kind of data in the database can be stored, byte value 0x00-0xff
If it proves to be as #10 said, then write a function.
Algorithm testing

$s =<<< TXT00 2D ef 8F A2 1 a The EF 9F bb 0000-XX 0E-EF 9F BB-6B-EF-9E-----3600 EF 9E B2 the EF 9E ba-xx ef 9E bayi xx 2FTXT; $s = preg_replace ("/[\r\n]/", ", $s); $s = pack (' h* ', $s); Constructs the binary data $m = 0;for ($i =0; $i
   
    
      2D E2 1 A (XX)--------------------0E 
     
2F

In addition to ask, so the conversion of $c is a character form printing, if you write back in hexadecimal file, how to deal with it?
I use
to $raw [$j ++]= $c;//raw is the raw data received
No, should I convert it again?
I want to write the file directly with $raw.

Binary data contains a lot of non-printable characters, in order to see them visually, it is generally printed in a byte two hexadecimal number format (such as the software in your map)

The hexadecimal string is converted to binary data, and the generally available pack (' h* ', ' ffffff ')
php5.4 also provides a hex2bin
Echo hex2bin ("6578616d706c65206865782064617461 "); Example hex data

Thank you very much for learning

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.