echo BLOBs from database (utf-8)
I'm currently working on a web-app with a utf-8 MYSQL database. There are some images saved as BLOBs which I need to display.
In all my naivety i thought it would just be something like that:if($image = $result->fetch_object())
{
header('Content-Type: {$image->mimetype}');
echo $image->data;
}
Which doesn't work. To me it seemed like the image-data is converted to utf-8. So I thought I simply do a dirty hack like
echo utf8_decode($image->data);
... again without any luck.
After that I removed the header(...) call to compare the binary result againstecho file_get_contents('sameImageAsInDatabase.jpg');
And there are some tiny differences
file_get_contents(...) - works:
database - doesn't work:ÿØÿàJFIFddÿìDuckyPÿá ¨http://ns.adobe.com/xap/1.0/ © 2010 Boston University all rights reserved ÿîAdobedÀÿÛ„ ÿÀÌÙÿÄì !1AQ"aq‘¡2#±ÁÑBRbr‚’3$ðáñ¢²CS%Òcƒ4Âs“£³DT& dt„5ÃÓ”EU6¤´ÔuV'âòÄ…W!1AQaq"ð‘¡±ÁÑ2BRáb#3ñr‚’¢²ÂCS$Òc4sƒ“³%â£Td5òDÿÚ?©Ÿ”²XãÆ6z:¿üWöyíÄTš/_þ+ÿXà¨P0Ž&Y?¬q·+ûõEXªÌ?ß?»Qý¸*ƒÃªÿ8*À3ï¯Y«ï?·X¬¿ñŸÝ¨œF¡t†G̦‘I,àµ?£<ÉÒ.=·¿_¦ˆ
º@s-¤Óé
p®d¨vÝ—¿ôÒ6• E%äú`wÒ9ÇØ×Ç;ÑTsé‡oÒFÅciBÕìxÇÛÝ®ÒÇâ0Òƒ‡fX/Û¾¼jsÖêÁâ2t Åí¨q–íǶb?@r (ø_fî§jxÌÿ·¶F¿Ã;8ÿݤϑšOúX5°¢þÙÇþèßdÿ¥ƒS ³Û{9Ïå3ñêIÿKªAD%›µöf÷fZŽRÉÿK¹"päso«l;{¸6¶Á嶂òÅçsÔfÔâM#í@bau·FD ¨V {zÙüÔ‡úGöâýLJ ¦¼¼5Ä‚¿Ê?·R(nîùÜH¤n ‘Dnnûù¬ß·P¡±suÿÄI_çŸÛ‰« 7Tÿ¼K_çœF Òl\ÜÇ“ÙæoÛ‚¡@3>_Þ$÷ëoÛˆ¨´Aÿ31ûÌ™sÖpUFnn?ø™s<5ŸÛ‚ ƒ>fæŸ÷‰<‹Û‚¬šóñëÉŸòÏíÁˆQëÏÿÄIîÔpU…¦¸¹§–âN5 1ý¸*ÈhRë/ïývý¸š…u®™=žvý¸*ÂŒ!ÖW•ek™‹åF¥GÇB†æ|¹”ø‚çႬƒ ÄõÎâPyùš‡
ÿØÿàJFIFddÿìDuckyPÿá ¨http://ns.adobe.com/xap/1.0/ © 2010 Boston University all rights reser开发者_StackOverflow社区ved ÿîAdobedÀÿÛ? ÿÀÌÙÿÄì !1AQ"aq?¡2#±ÁÑBRbr??3$ðáñ¢²CS%Òc?4Âs?£³DT& dt?5ÃÓ?EU6¤´ÔuV'âòÄ?W!1AQaq"ð?¡±ÁÑ2BRáb#3ñr??¢²ÂCS$Òc4s??³%â£Td5òDÿÚ?©??²XãÆ6z:¿üWöyíÄT?/_þ+ÿXà¨P0?&Y?¬q·+ûõEXªÌ?ß?»Qý¸*?êÿ8*À3ï¯Y«ï?·X¬¿ñ?ݨ?F¡t?G̦?I,àµ?£<ÉÒ.=·¿_¦?
º@s-¤Óé
p®d¨vÝ?¿ôÒ6? E%äúwÒ9ÇØ×Ç;ÑTsé?oÒFÅciBÕìxÇÛÝ®ÒÇâ0Ò??fX/Û¾¼jsÖêÁâ2t Åí¨q?íǶb?@r (ø_fî§jxÌÿ·¶F¿Ã;8ÿݤÏ??OúX5°¢þÙÇþèßdÿ¥?S ³Û{9Ïå3ñêIÿKªAD%?µöf÷fZ?RÉÿK¹"päso«l;{¸6¶Áå¶?òÅçsÔfÔâM#í@bau·FD ¨V {zÙüÔ?úGöâýLJ ¦¼¼5Ä?¿Ê?·R(nîùÜH¤n ?Dnnûù¬ß·P¡±suÿÄI_ç?Û?« 7Tÿ¼K_ç?F Òl\ÜÇ?ÙæoÛ?¡@3>_Þ$÷ëoÛ?¨´Aÿ31ûÌ?sÖpUFnn?ø?s<5?Û? ?>fæ?÷?ÍGXÔ
?Ü?Ì?Þíûq5aFk©qJüÔ?×?· Ò?î³¥Ä??5Û?©@MÍ×üiôíÁP us?3Ëìó?Û?¡@1qÿÄKìóÛ?¤?ù??ÿ¼IýsûpT(¯sÇæ$>vý¸?lÜ\
Anyone knows what's going on?
Don't do utf encoding/decoding - you're dealing with BINARY data, not TEXT data. By chance you'll probably end up with some byte sequences in the binary data that LOOK like utf code points, and the encode/decode sequence will corrupt your file.
精彩评论