Heim >Backend-Entwicklung >PHP-Tutorial >PHP的base64_decode及写入mysql前的转义

PHP的base64_decode及写入mysql前的转义

WBOY
WBOYOriginal
2016-06-23 13:54:483755Durchsuche

我看到这个函数解码后返回一个string。
现在我编码前是一个二进制文件流,从图像文件读取来的。如果用这个函数解码并赋值给一个变量,那么应该默认就是一个string变量了。string变量可以存储二进制文件流吗?如果不能,应该怎么做,才能正确解码呢?

另外一个问题是,解码后要将这个二进制流存入到mysql的一个longblob字段里面,我需要对哪些字符进行转义?PHP是不是有专门的函数来一次性实现这个转义的?

补充一下我所用的编码和解码方式:

编码部分(VC端)

char buffer[102400];  //BUF_SIZE大小自己定义DWORD length;//照片图片FILE * pFile = fopen("photo.bmp", "rb");int img = fread(buffer, sizeof(char), 102400, pFile);//将文件二进制流读入到buffer中fclose(pFile);CryptBinaryToString((const BYTE *)buffer,img,CRYPT_STRING_BASE64,NULL,&length);//获取存放编码后字串所需长度ImgString = new TCHAR[length];//ImgString是要发送给PHP的字串,根据上面获取的长度,初始化该字串length--;//减去最后字串结束符的位置CryptBinaryToString((const BYTE *)buffer,img,CRYPT_STRING_BASE64,ImgString,&length);//向ImgString里面存放buffer的base64编码


解码并写入数据库部分(PHP+Mysql)

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


回复讨论(解决方案)

印象中二进制文件和字符串没什么不同啊..

转义用addslashes或者mysql_real_escape_string

我得到的出错提示:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\0\0\0\0\0\06\0\0\0(\0\0\0f\0\0\0~\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0' at line 1

印象中二进制文件和字符串没什么不同啊..

转义用addslashes或者mysql_real_escape_string


。。。二进制里面很多字串结束符的啊

那个出错提示里的错误位置,比mysql请求语句里面尝试写入的二进制流要短得多啊,不是已经转义过了吗,怎么还会截断?

请求里面是这个:
face=BMΗ\0\0\0\0\0\06\0\0\0(\0\0\0f\0\0\0~\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0 RZp?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????{??!0 \0\0 RZp??????????????w\\?|???\'?+??????????????????.?[?.+?{???z?z???????np?.0w|?????????????290ryp????|L?\\?|}??.??N???? 1?\00Q\0?q???1! ?`?pP?`@?P0?@ ?P1\0PA pa`??`??`??`??`??`??\0\0?P`?PP?PP?@@?0@? 0? 0? ? ? ? 0?0A\0@Q`Q pa0?qP??`??p??`??`??`??q\0?Q\0?a?……………………后面还有好长

行进中的是流,静止下来就是字符串
二进制文件和文本文件的区别在与前者没有文件结束符 ox1a
既然你发送的是 base64 编码所谓数据,那么入库时不需要解码,并且也不需要转义
待日后还原数据时再解码不迟


行进中的是流,静止下来就是字符串
二进制文件和文本文件的区别在与前者没有文件结束符 ox1a
既然你发送的是 base64 编码所谓数据,那么入库时不需要解码,并且也不需要转义
待日后还原数据时再解码不迟


即使不解码、不转义,也是错的。

Invalid query: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Qk3OlwAAAAAAADYAAAAoAAAAZgAAAH4AAAABABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAcID' at line 1Whole query: update index_base set cardtype=1, name='杨妍', sex='0', nationality='汉', birth='19850906', address='南京市鼓楼区广州路213-1号',expiration='20210602', expirebegin='20110602',  cardid='321283198509060027',issued='南京市公安局鼓楼分局',SAMID= '05.03-20131218-0001645552-2769328138',IINSNDN='0000000000000000',face='Qk3OlwAAAAAAADYAAAAoAAAAZgAAAH4AAAABABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAcIDwYGDgUFDgUFEAQFDwMEDgIDDQECDQECDQECDgIDDwMEEAQFEQYFEgcGEwgHFQoJFgsKFwwLFg4MFg8NGRIQHBUTHBcVHxoYHBYXGhMXFw8XFwsYDQgYBAUZUlpwo7DJqLXOrbn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7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /v7 /gAA妄?????? where id=55556

不解码直接写入成功了。单引号改成双引号就可以了。
然后我试了下用同样的方法写入解码之后的值,也比以前好一点,至少这个长串数据前面有一段能正确识别,mysql报错的位置从数据串中间开始的。

不知道我解码再转义后是否能正确?

base64_encode后的就是字符串了,直接用base64_encode后的入?,出的?候再用base64_decde ??一次就可以了。

尾部的 .../v7 /v7 /gAA妄??????  哪里来的?
 这些是图片数据?显然是未经base64 编码的,因为 base64 编码中只有 数字、大小写字母和 /-= 3个符号

行进中的是流,静止下来就是字符串
二进制文件和文本文件的区别在与前者没有文件结束符 ox1a
既然你发送的是 base64 编码所谓数据,那么入库时不需要解码,并且也不需要转义
待日后还原数据时再解码不迟


我把二进制写进去了……但是图片完全损坏了。
在NaviCat里面可以看到图片的样子,身份证头像脖子领口以上完全歪到图片外面去了,没歪出去的部分也是很多彩色噪点,基本分辨不出来是什么。
如果把二进制字段另存为一个文件,那就根本打不开。

尾部的 .../v7 /v7 /gAA妄??????  哪里来的?
 这些是图片数据?显然是未经base64 编码的,因为 base64 编码中只有 数字、大小写字母和 /-= 3个符号


我在VC端调试看了发送前的编码串,就是那个样子,看来是我编码不对了啊?

base64 编码只不过是将 3 个字节扩展成 4 个字节,是的每个字节的有效二进制位为 6 位,即十进制 0 ~ 63 供 64 种状态,故名 base64
我不是很清楚你做编码是使用库函数,还是自己写的代码
但从生成的串上看,肯定是弄错了

base64 编码只不过是将 3 个字节扩展成 4 个字节,是的每个字节的有效二进制位为 6 位,即十进制 0 ~ 63 供 64 种状态,故名 base64
我不是很清楚你做编码是使用库函数,还是自己写的代码
但从生成的串上看,肯定是弄错了


我用了wincrypt.h和crypt32.lib。而且两个是在不同地方下载的。会不会版本不一样哦。明天去公司看看。

在网上找个Base64编码的C源代码试试吧,很多啊。而且有很多在线编码的网页可以用来验证编码和解码是否正确:http://www1.tc711.com/tool/BASE64.htm

base64 编码只不过是将 3 个字节扩展成 4 个字节,是的每个字节的有效二进制位为 6 位,即十进制 0 ~ 63 供 64 种状态,故名 base64
我不是很清楚你做编码是使用库函数,还是自己写的代码
但从生成的串上看,肯定是弄错了


是在C语言里编码的时候,长度少了一位,把字符串结束符给截掉了。
改成JpgBack = new TCHAR[length+1];,后面的lentth--删掉,就没有乱码了。

但是还有个问题怎么解决啊,我得到的字串每64个字符就有一个换行。怎么把换行截掉?

对应该是 需要的长度 + 1个结束位
每64个字符就有一个换行?是一个回车一个换行吧?
这是正常的传输格式(RFC 2045),目的是能快速校正传输中的漏码
到解码时删掉就行了

对应该是 需要的长度 + 1个结束位
每64个字符就有一个换行?是一个回车一个换行吧?
这是正常的传输格式(RFC 2045),目的是能快速校正传输中的漏码
到解码时删掉就行了


我现在已经找到办法删掉回车和换行了。
但是,但是又出问题了。。。

传输过去之前的编码是正常的,我能用网上找到的编解码器恢复成图像。
而且我是特地断点检查了发送前的postdata字串里面的,都是对的。
但是一旦传递到PHP这边,我用$face = $_REQUEST['face'];接收传参,立刻die ($face),结果出来的字串,所有的加号(+)全部被替换成了空格。

这是怎么回事啊

我知道这些都能在日后读取的时候处理,但是我们老板不懂技术,我最好是从mysql里面复制出来直接能解码给他看。。。

+ 变成 空格 是 URL 解码的功劳
$face = str_replace(' ', '+', $_REQUEST['face']);
换回来就是了

$img = base64_decode($face);
还原回图片数据了

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn