Tracker7
Tracker7

Reputation: 87

Can't display blob image with base64_encode

I'm having problem with displaying image which is stored in a database as a blob. In localhost it works perfect, but on the server it doesn't show anything. Could someone help me please. I've already surfed the internet to find the answer, and read many forums but still couldn't find the answer.

Here is my code.

<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 some image";

                else 
                {
                  $tmpImageName=$_FILES['image']['tmp_name'];
                  $image_name = addslashes($_FILES['image']['name']);
                  $image_size = getimagesize($_FILES['image']['tmp_name']);
                  $handle      = fopen( $tmpImageName, 'r' );
                  $content = fread( $handle, filesize( $tmpImageName ) );
                  fclose($handle);

                  if($image_size==FALSE)
                    echo "That's not an image";

                  else 
                  {

                    $sql="insert into image values ('','$image_name','".mysql_real_escape_string($content)."')";

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


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

                }

                ?>

Upvotes: 0

Views: 2392

Answers (1)

Deepak Singh
Deepak Singh

Reputation: 460

try

 echo '<img src="data:image/jpeg;base64,'.base64_encode($row['image']) .' "/>';

Upvotes: 1

Related Questions