How PHP receives binary files to replace content inside

Source: Internet
Author: User
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);
}

  • 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.