title: Getting broken image retrieving BLOB with PHP
body: <p>Just for the record, this is being done with Wordpress (I Didn't choose it nor to store the images in a DB either).</p>
  
  <p>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).</p>
  
  <p>This is how i store the image:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;input type="file" name="img" id="media"&gt;
  </code></pre>
  
  <p>PHP:</p>
  
  <pre><code>$item['media'] = addslashes (file_get_contents($_FILES['img']['tmp_name']));
  $result = $wpdb-&gt;insert($table_name, $item);
  </code></pre>
  
  <p>And this is how im trying to retrieve it:</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;img src="Image_View.php?image_id=&lt;?php echo $item["item_id"]; ?&gt;"/&gt;
  </code></pre>
  
  <p>Image_View.PHP:</p>
  
  <pre><code>&lt;?php
    global $wpdb;
    $result =  $wpdb-&gt;get_row("SELECT media FROM $wpdb-&gt;item WHERE item_id = 3", ARRAY_A);
    header("content-type: image/png");
    echo $result["media"];
  ?&gt;
  </code></pre>
  
  <p>BTW! this is my first question in Stackoverflow! so HI EVERYONE! </p>
  
  <hr>
  
  <p><strong>Edit 1:</strong></p>
  
  <p>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:</p>
  
  <p>GET <a href="http://127.0.0.1/pymes/wp-admin/Image_View.php" rel="nofollow">http://127.0.0.1/pymes/wp-admin/Image_View.php</a> 500 (Internal Server Error)</p>
  
  <p>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.</p>
  
  <hr>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>Again! sorry for being late on this, i've been working</p>
  
  <p>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).</p>
  
  <p>Regarding showing the image, i was having troubles with Headers being sent before it could make it to:</p>
  
  <pre><code>header("content-type: image/png");
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>&lt;?php
  global $wpdb;
  $result =  $wpdb-&gt;get_row("SELECT media FROM $wpdb-&gt;item WHERE item_id = $_GET[id]", ARRAY_A);
  
  echo '&lt;img src="data:image/png;base64,'.base64_encode($result['media']).'" height="100" width="100"/&gt;';
  ?&gt;
  </code></pre>
  
  <p>By doing this I don't longer need the Image_View.php file.</p>
  
  <p>Really appreciate the help provided here, TY guys!  </p>