Question

Having problems with inserting image into table of blob type. If I insert it manually by phpmyadmin, and print it, I can get the image, but with this code I can't insert into table. On localhost it works, but on the server doesn't. Can you please help. I've already searched the forums, but couldn't get correct answer. Here's the 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 
                {
                  $image_name = mysql_real_escape_string($_FILES['image']['name']);
                  $image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));

                  $image_size = getimagesize($_FILES['image']['tmp_name']);




                  if($image_size == FALSE)
                  {
                    echo "That's not an image";
                  }
                  else
                  {
                    if (!$insert = mysql_query("insert into image(id, name, image) values ('','$image_name','$image')"))
                    {                                                        
                      echo "Problem uploading image";
                    }                       
                    else
                    {                                                      

                      $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;'>";
                      }
                    }

                  }

                } 

                ?>

If I echo the $row['Image'] the result is like this: "?PNG IHDR??vlH cHRMz%??????u0?`:?o?" and etc.

Was it helpful?

Solution 3

Solved in PDO

$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;'>";
          }

OTHER TIPS

You have not even used image_size in your so don't need to define it, and on other hand you are not storing the file path into db. first define file path of the image and then store it, else refer this Reference site

and Update your query

("insert into image(id, name, Image) values (null,'$image_name','$image')")

I think you are not opening a database connection before querying

$con = mysql_connect("localhost","mysql_user","mysql_pwd");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
$query = "your query";
mysql_query($query,$con);

and also use

header("Content-type: image/jpeg");

before echoing the image.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top