Tracker7
Tracker7

Reputation: 87

Troubles with image uploading using PDO

I'm having some problems with the upload of image. I want to store and retrieve image from the table, but when I try to get the image, it doesn't show. There is only broken image's icon. However, if I upload image manually by phpmyadmin, it shows the image. My table row's type is blob. I'm totally confused. Can anyone help please.

My code is simple:

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

Upvotes: 0

Views: 751

Answers (1)

Alexey Palamar
Alexey Palamar

Reputation: 1430

Try to delete validation, you use prepaired statement, you don't need it.

              $pdo = new PDO('mysql:dbname=database_name;host=localhost', 'username', 'password',
                array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

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

              $stmt = $pdo->prepare('INSERT INTO image (name,image) VALUES (:name,:image)');
              $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;'>";
              }

Upvotes: 1

Related Questions