1

Just for the record, this is being done with Wordpress (I Didn't choose it nor to store the images in a DB either).

Basically I'm trying to store and image in a Database using php and showing it later, but i am getting a broken image icon. (trust me i've been researching this and I can't find what's wrong).

This is how i store the image:

HTML:

<input type="file" name="img" id="media">

PHP:

$item['media'] = addslashes (file_get_contents($_FILES['img']['tmp_name']));
$result = $wpdb->insert($table_name, $item);

And this is how im trying to retrieve it:

HTML:

<img src="Image_View.php?image_id=<?php echo $item["item_id"]; ?>"/>

Image_View.PHP:

<?php
  global $wpdb;
  $result =  $wpdb->get_row("SELECT media FROM $wpdb->item WHERE item_id = 3", ARRAY_A);
  header("content-type: image/png");
  echo $result["media"];
?>

BTW! this is my first question in Stackoverflow! so HI EVERYONE!


Edit 1:

As hanshenrik mentioned, $wpdb->insert is escaping the input, so i uploaded and image then wrote it to disk, set the proper exntension and BOOM there you go, now i know im actually uploading the image the proper way, but it looks like im still having troubles trying to show it, when using chrome's developer tool im getting this:

GET http://127.0.0.1/pymes/wp-admin/Image_View.php 500 (Internal Server Error)

So, is something wrong with the Image_View.php ? the query is actually good and is retrieving the data since i copied and pasted that code in another place and it echoed the whole lote of characters inside of the bin file.


Edit 2:

Again! sorry for being late on this, i've been working

So, the solution for my problem while uploading the image has been explained in Edit 1 (thanks to hanshenrik for suggesting that wpdb insert was escaping the input).

Regarding showing the image, i was having troubles with Headers being sent before it could make it to:

header("content-type: image/png");

so i was struggling at this and I decided to try using URI for the image, which solved my problem. For me this is more a workaround rather than an actual fix, but it floated my boat. the way i implemented the URI is as follows:

<?php
global $wpdb;
$result =  $wpdb->get_row("SELECT media FROM $wpdb->item WHERE item_id = $_GET[id]", ARRAY_A);

echo '<img src="data:image/png;base64,'.base64_encode($result['media']).'" height="100" width="100"/>';
?>

By doing this I don't longer need the Image_View.php file.

Really appreciate the help provided here, TY guys!

VdeVentura
  • 2,041
  • 2
  • 15
  • 16
  • 2
    Hi back! I think the most likely thing is that the image has been corrupted by the `addslashes (file_get_contents($_FILES['img']['tmp_name']));` when you stored it – RiggsFolly Aug 26 '15 at 15:06
  • I think I might have converted the image to `base64` and then stored that if I had to store an image on a MYSQL database. [See this](http://stackoverflow.com/questions/3967515/how-to-convert-image-to-base64-encoding) – RiggsFolly Aug 26 '15 at 15:11
  • Simple test, read the blob from the database, write it to disk with the correct extn. If you cannot open it with a image viewer, then it got naffed by the store proceedure. – RiggsFolly Aug 26 '15 at 15:20
  • you are right!, i already tried changing the extension and i couldn't open it. Sorry for being late, i didnt thought someone was going to answer me as i saw the question going down and down hhaha – VdeVentura Aug 26 '15 at 15:30
  • So, what could i do, it would be great if u know of any guide that i could follow, im kinda new to this and in most of the examples that i found they used the addslashes funtion @RiggsFolly – VdeVentura Aug 26 '15 at 15:33
  • Maybe this http://stackoverflow.com/questions/1636877/how-can-i-store-and-retrieve-images-from-a-mysql-database-using-php – RiggsFolly Aug 26 '15 at 15:38
  • ok, i took a look at the link in your first comment, let me see if i got this right, i should use something like this: $base64 = base64_encode($imagedata); instead of the addslashes and store its result ? – VdeVentura Aug 26 '15 at 15:38
  • Or this tutorial http://www.phpro.org/tutorials/Storing-Images-in-MySQL-with-PHP.html which does nothing with the image before storing it. – RiggsFolly Aug 26 '15 at 15:42
  • Cannot be any more help, I have never been in the situation of having to do this. I woudl always store images on disk with just a reference to them in the database – RiggsFolly Aug 26 '15 at 15:44
  • Ok, i'll try those and report, TY very much bro! @RiggsFolly – VdeVentura Aug 26 '15 at 15:44
  • Yea I would love to know what actually was wrong and even more what actually works – RiggsFolly Aug 26 '15 at 15:45
  • i don't know if i should be doing this but @RiggsFolly question updated. – VdeVentura Aug 26 '15 at 16:49

2 Answers2

0
$item['media'] = addslashes (file_get_contents($_FILES['img']['tmp_name']));
$result = $wpdb->insert($table_name, $item);

addslashes? are you crazy? use a proper SQL escape function, if $wpdb->insert does not escape for you.

anyway, with your current code, this might work..

 echo stripslashes($result["media"]);

or perhaps

echo htmlspecialchars(stripslashes($result["media"]));
hanshenrik
  • 19,904
  • 4
  • 43
  • 89
  • WOW! looks like you nailed it! apparently $wpdb->insert does escape de data! maybe that was the trouble =# ill try this! – VdeVentura Aug 26 '15 at 16:12
  • data: (array) Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). From wp codex – VdeVentura Aug 26 '15 at 16:13
  • ok! so far so good, i skipped the addslashes and now when i write to the disk the image and set the proper extension i can actually see it (something i couldn't do before!) – VdeVentura Aug 26 '15 at 16:18
  • i don't know if i should be doing this but @hanshenrik question updated. – VdeVentura Aug 26 '15 at 16:49
  • ..now i want to see the exact html generated by your script :p – hanshenrik Aug 26 '15 at 20:13
  • sorry for being late i managed to solve this, ill update the question explaining how i did it asap. @hanshenrik – VdeVentura Aug 27 '15 at 20:16
0

So, the solution for my problem while uploading the image has been explained in Edit 1 (thanks to hanshenrik for suggesting that wpdb insert was escaping the input).

Regarding showing the image, i was having troubles with Headers being sent before it could make it to:

header("content-type: image/png"); so i was struggling at this and I decided to try using URI for the image, which solved my problem. For me this is more a workaround rather than an actual fix, but it floated my boat. the way i implemented the URI is as follows:

get_row("SELECT media FROM $wpdb->item WHERE item_id = $_GET[id]", ARRAY_A);

echo ''; ?> By doing this I don't longer need the Image_View.php file.

Really appreciate the help provided here, TY guys!

VdeVentura
  • 2,041
  • 2
  • 15
  • 16