How PHP receives binary files how to replace content inside
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!
PHP Binary
------Solution--------------------
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.
------Solution--------------------
This post was last edited by xuzuning on 2013-05-02 15:35:02
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);
}