0

I have JPEG/PNG small file which is stored in the mysql database field picture as following:

  $fdata = '';
  if (isset($_FILES['picture']) && $_FILES['picture']['size'] > 0) {
    $tmpName  = $_FILES['picture']['tmp_name'];  
    $fp      = fopen($tmpName, 'r');
    $fdata = fread($fp, filesize($tmpName));
    $fdata = addslashes($fdata);
    fclose($fp);
    //$sl = "INSERT INTO image (image)VALUES ( '$data')", $connection);
    print "Thank you, your file has been uploaded.";        
  } else {
    print "No image selected/uploaded";        
  }

  // Update the table 
  $this->db->update('users', 
                    array('password' => $_POST['password'], 
                          'picture' => $fdata), 
                    array('username=?' => $ouser )); 

But problem is now how do i output that "picture field" value from the database into real picture for web browsers?

EDIT 1:

Simply echo does not render the picture in browser

enter image description here

EDIT 2:

enter image description here

  public function pictureshowAction() {  
    $this->_helper->layout()->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    $this->_response->setHeader('Access-Control-Allow-Origin', '*');
    $this->db = Application_Model_Db::db_load();        
    $ouser = $_GET['ousername'];

    $sql = "select *From users where username='{$ouser}' limit 1";
    $result = $this->db->fetchAll($sql);
    if (count($result) > 0 ) {
      $picture  = $result[0]['picture'];     
      //$content = $picture; 
      $content = stripslashes($picture);
    } else {
      $content = '';
    }

    //echo $content;    

    $this->getResponse()
            ->setHeader('Content-Type', 'image/jpg')
            ->setBody($content)
            ->sendResponse();    
    exit;
  }   
  • possible duplicate of [php: recreate and display an image from binary data](http://stackoverflow.com/questions/2070603/php-recreate-and-display-an-image-from-binary-data) – mopo922 Jan 15 '15 at 06:05

1 Answers1

0

Very easy, let's say your content type was png you first need to send a proper header to browser

header('Content-type: image/png');

Then simply output your picture content

echo $pictureContentFromDatabase;

What your screenshot shows is caused by the fact that you did not send a proper Content Type header telling the browser what your server is sending is an image, once you fix that it will be fine. And you dont have to addslashes for output, that will render the image useless. So echo the content without addslashes

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • So i have to remove the addslashes from $pictureContentFromDatabase right? e.g: `$this->getResponse() ->setHeader('Content-Type', 'image/png') ->setBody($pictureContentFromDatabase) ->sendResponse();` –  Jan 15 '15 at 06:12
  • Please see my EDIT 2 (code that i am trying), i tried to echo with image/jpg but the browser not showing expected picture shown on left –  Jan 15 '15 at 06:25
  • 1
    Now that's another issue as long as the image is displaying :) Will have to do more research to find out why the resolution is different than expected. – Hanky Panky Jan 15 '15 at 06:32
  • NO - i mean on my right side the image is not showing at all its a small tiny image box showing in Google chrome, but the expected image should be shown which is on the left side. –  Jan 15 '15 at 06:37
  • 1
    Whats the output of `var_dump($content);`? – Hanky Panky Jan 15 '15 at 06:39
  • Its working now. i have to store it as base64_encode e.g: `// not this $fdata = addslashes($fdata); but this $fdata = base64_encode($fdata);` and then show in the browser as `echo "";` –  Jan 15 '15 at 06:57