-2

I am attempting to recall an image from the database. I have established the image reaches the database properly, however when I pull the image down I get the Blob text that makes it up. How can I pull down the image in order to display the image?

<?php
    //libs
    include 'library/config.php';
    include 'library/opendb.php';
    //query
    $query = "SELECT * FROM `upload` WHERE `postID`=".$elem["postID"].";";
    $result = mysqli_query($conn, $query);
    if (mysqli_num_rows($result) > 0) { // if results exist
        while ($row = mysqli_fetch_assoc($result)) { // assign db cont to variable
            $name = $row["name"];
            $size = $row["size"];
            $type = $row["type"];
            $content = $row["content"];
            // header info
            header("Content-length: $size");
            header("Content-type: $type");
            header("Content-Disposition: attachment; filename=$name");
            //display image
            echo "img", $content;
         }
         include 'library/closedb.php';
     ?>

2 Answers2

1

use

   $content = base64_encode($_row['content']);

to display a jpeg image.

http://php.net/manual/en/function.base64-encode.php

Loveen Dyall
  • 824
  • 2
  • 8
  • 20
0
$code_base64 = $row['content'];
$code_base64 = str_replace('data:image/jpeg;base64,','',$code_base64);
$code_binary = base64_decode($code_base64);
$image= imagecreatefromstring($code_binary);
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);