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!