PHP base64_decode and escape before mysql

Source: Internet
Author: User
PHP base64_decode and the escape Post before writing it to mysql are edited by dimply from 2014-06-1919: 17: 39. I can see that this function is decoded and a string is returned. Now I am encoding a binary file stream, which is read from the image file. If you use this function to decode and assign a value to a variable, it should be a string variable by default. S PHP base64_decode and escape before mysql writing

This post was last edited by dimply at 19:17:39

The decoded function returns a string.
Now I am encoding a binary file stream, which is read from the image file. If you use this function to decode and assign a value to a variable, it should be a string variable by default. Can string variables store binary file streams? If not, how can we decode it correctly?

Another problem is that after decoding, the binary stream will be saved to a longblob field in mysql. which characters do I need to escape? Does PHP have a special function to implement this escape at one time?

Add the encoding and decoding methods I used:

Encoding part (VC end)

Char buffer [102400]; // custom BUF_SIZE
DWORD length;

// Photo and image
FILE * pFile = fopen ("photo.bmp", "rb ");
Int img = fread (buffer, sizeof (char), 102400, pFile); // read the binary stream of the file into the buffer.
Fclose (pFile );

CryptBinaryToString (const BYTE *) buffer, img, CRYPT_STRING_BASE64, NULL, & length); // Obtain the length of the string after encoding.
ImgString = new TCHAR [length]; // ImgString is the string to be sent to PHP. the string is initialized based on the length obtained above.
Length --; // minus the position of the final string terminator
CryptBinaryToString (const BYTE *) buffer, img, CRYPT_STRING_BASE64, ImgString, & length); // store the base64 encoding of the buffer into ImgString


Decoding and writing data to the database (PHP + Mysql)

$face = addslashes(base64_decode(trim($_REQUEST['face'],"'")));
$query = "update index_base set face=$face where id=$bid";
$result = mysql_query($query);

------ Solution --------------------
Find a base64-encoded C source code on the Internet. And there are a lot of online coding web pages can be used to verify the encoding and decoding is correct: http://www1.tc711.com/tool/BASE64.htm
------ Solution --------------------
It should be the required length + 1 second bit
Is there a line break for every 64 characters? Is it a carriage return and a line feed?
This is a normal transmission format (RFC 2045), which aims to quickly correct missing codes during transmission.
You can delete it when decoding.
------ Solution --------------------
+ Converting to space is a credit for URL decoding.
$ Face = str_replace ('', '+', $ _ REQUEST ['face']);
Just change it back.

$ Img = base64_decode ($ face );
Restored image data

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.