Jump to content

imagepng() -> MySQL BLOB


Kryptix

Recommended Posts

I have created a image using imagepng() and then right-click'd and saved it. I then added it to the database by using phpMyAdmin to upload it directly into a field called 'avatar' which is a BLOB. The code the view the image back is the following:

 

header("Content-type: image/png");
echo mysql_result(mysql_query("SELECT `avatar` FROM `rscd_players` WHERE `username` = 'Kryptix'"), 0);

 

Now when I try and add the imagepng() directly to the database it doesn't work. I've tried multiple things. Here's the code:

 

ob_start(); imagepng($char_image); $pngimagedata = ob_get_contents(); ob_end_clean();

mysql_query("UPDATE `rscd_players` SET `avatar` = " . $pngimagedata . " WHERE `username` = 'Kryptix'");

 

Here's the full code: http://pastebin.com/jqsKc9Qd

 

This is the query that phpMyAdmin produces: http://pastebin.com/nuypNuwJ

 

This is the query that the above code produces: http://pastebin.com/NPWmi5eb

 

phpMyAdmin is 2.2kB, my code is 4.4kB

 

Any idea? I've been trying all night...

Link to comment
Share on other sites

$pngimagedata is raw, binary data. You can't insert it into a text query and expect it to work. What if the binary data contains quotes? Valid SQL statements? It's like some rocket scientist explaining propulsion to a layman: he might follow along for a second or two, but he'll get confused very fast, and he might even misinterpret something because it sounded familiar.

 

You should use mysqli or PDO instead of the plain mysql extension, but if you're stuck with it then use mysql_real_escape_string on $pngimagedata. Just like you would with form inputs. Because in the end it's all untrusted data.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.