2

I'm trying to insert the image into mysql database table directly. In my database I'm always getting [BLOB - 0B]. it doesn't insert images into table. I didn't get any error too. I'm confused..

PHP

ini_set('display_startup_errors',1);
    ini_set('display_errors',1);
    error_reporting(-1);

    include('config.php');
     if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) 
      { 
          $tmpName  = $_FILES['image']['tmp_name'];  

          $fp = fopen($tmpName, 'r');
          $data = fread($fp, filesize($tmpName));
          $data = addslashes($data);
          fclose($fp);
      } 

      try
        {
            $stmt = $conn->prepare("INSERT INTO images ( picture ) VALUES ( '$data' )");
//          $stmt->bindParam(1, $data, PDO::PARAM_LOB);
            $conn->errorInfo();
            $stmt->execute();
        }
        catch(PDOException $e)
        {
            'Error : ' .$e->getMessage();
        }

HTML

<form action="upload.php" method="post">
<input id="image" name="image" type="file" />
<input type="submit" value="Upload" />
</form>
Karuppiah RK
  • 3,894
  • 9
  • 40
  • 80

1 Answers1

7

You almost got it, you want PDO::PARAM_LOB to be a file pointer which you created above, not the result of reading the fp

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) 
{ 
   $tmpName  = $_FILES['image']['tmp_name'];  
   $fp = fopen($tmpName, 'rb'); // read binary

   $stmt = $conn->prepare("INSERT INTO images ( picture ) VALUES ( ? )");
   $stmt->bindParam(1, $fp, PDO::PARAM_LOB);
   $stmt->execute();
} 
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Victory
  • 5,811
  • 2
  • 26
  • 45
  • no. now it doesn't insert any datas into the field. Now I didn't get `BLOB-0B` in my table.. – Karuppiah RK May 25 '14 at 10:05
  • @Beginner - are you sure that the `$_FILES` array is populated? – Victory May 25 '14 at 10:10
  • oh sorry. problem is not in my php coding. this is my fault. I missed to mention this on my form `enctype="multipart/form-data"` . anyway thanks mate... – Karuppiah RK May 25 '14 at 10:16
  • @Beginner - if this answers your question you should +1 and accept. You could also edit your post to mention the `enctype` change. – Victory May 25 '14 at 10:17