1
votes

I'm having problem with showing image which is stored in database as a blob. I searched in many forums, but couldn't find any answer.

I separated the code into two files. In index.php i insert image and try to load it from get_image.php file.

Here is index.php

<form action="index.php" method="POST" enctype="multipart/form-data">
                  File:
                  <input type="file" name="image"> <input type="submit" value="upload">
                </form>

                <?php 
                  $file = $_FILES['image']['tmp_name'];

                  if(!isset($file)){
                    echo "Please select an image.";
                  }

                  else{
                    $image_name = mysql_real_escape_string($_FILES['image']['name']);
                    $image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
                    $imageType = mysql_real_escape_string($_FILES['image']['type']);
                    $image_size = getimagesize($_FILES['image']['tmp_name']);


                    if($image_size == FALSE){
                      echo "That's not an image";
                    }
                    else{
                      if (!$insert = mysql_query("UPDATE 'table_name' SET `image` = '$image' WHERE 'row_name' = '$student_id'")){                                                        
                        echo "Problem updating image";
                      }

                      else{                                                      
                        echo "<img src=get_image.php>";
                      }

                    }
                  }
                ?>

And get_image.php

<?php 

        $query = mysql_query("SELECT image FROM table_name WHERE student_id = 51686");
        while($row= mysql_fetch_assoc($query)){
            $imageData = $row['image'];
        }

        header("Content-type: image/png");
        echo $imageData;        
?>

Can anyone help please. My php version is 5.2 At the output there is image icon, but no image. If I right click and try to save it as image, it's not an image format, it's saved as get_image.php. So I don't know what is the problem.

2
Try to change the content type to text/plain and open the image url directly. Look for spaces in front of the png code and for error messages. (Have you connected your dbms and selected an db in get_image.php?)urzeit
@urzeit Changed to text/plain no differencies, didn't get you, how to open image which is in Blob directly, i got it from db and put automatically, the png code starts with "?Jw?=wf?yf?y?)?". And yes I connected my db in get_image.phpTracker7
From this we can see, that the data is not valid png (which starts with \211PNG. So we have to look at the way you store and load from and to your db.urzeit
@urzeit I tried to insert image manually, and update it from this code, still no resultTracker7

2 Answers

0
votes

please try to insert record like this below

 $tmpImageName=$_FILES['files']['tmp_name'];
 $handle      = fopen( $tmpImageName, 'r' );
 $content = fread( $handle, filesize( $tmpImageName ) );
 fclose($handle);

 $sql='insert into imagetest(image)values("'.mysql_escape_string($content).'")';

 mysql_query($sql) or die(mysql_error());

and then try to display image like here

$res = mysql_query("SELECT * FROM imagetest"); 
 while ($row = mysql_fetch_assoc($res)) {
   echo "<img src=data:image/jpeg;base64," . (base64_encode(($row['image']))) . " >";
  }
0
votes
$pdo = new PDO('mysql:dbname=database_name;host=localhost', 'username', 'password',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

      $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
      $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
      $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

      $stmt = $pdo->prepare('INSERT INTO image (name,image) VALUES (:name,:image)');
      $stmt->bindParam(':image', $imageData, PDO::PARAM_LOB);
      $stmt->bindParam(':name', $imageName);
      $stmt->execute(array('name' => $imageName, 'image' => $imageData));
      echo "Image Uploaded";


      $res = mysql_query("SELECT * FROM image "); 
      while ($row = mysql_fetch_assoc($res)) 
      {                                                  
        echo "<img src=data:image/jpeg;base64," . (base64_encode(($row['image']))) . " style='width:60px;height:60px;'>";
      }