i can't insert photo in database

I have a big problem in insertion of photo (blob) into database with php. I can't execute query. It returns false. This is my code

public function addPhoto()
 {
   $ret        = false;
   $img_blob   = '';
   $img_titre    ='';
   $description = "hhhhh";
   $selogon = '';
   $ret= is_uploaded_file($_FILES['fic']['tmp_name']);

   if (!$ret) {
       echo "Problème de transfert";
       return false;
   } else {
       $img_titre  = $_FILES['fic']['name'];
       $img_blob = file_get_contents ($_FILES['fic']['tmp_name']);
       $conn = connection();
       $sql = "INSERT INTO image (titre,selogon,description,img_blob)
               VALUES ( '$img_titre','$selogon','$description','".addslashes ($img_blob)."')" ;

                 if ($conn->query($sql) === TRUE) {
                     echo "New record created successfully";

                     }
                     else {
                       echo "string";
                     }

       $conn->close();
 }

Upvotes: 2

Views: 190

Answers (2)

I have been solve the problem : solution : in augmentation of size of photo in type (blob --> longblob ) .

Upvotes: 0

Robert
Robert

Reputation: 20286

PDO solution

Assuming you're using PDO(not specified by you), if you want to save it as blob then following steps should be done

try
{
  $fp = fopen($_FILES['fic']['tmp_name'], 'rb'); // read the file as binary
  $stmt = $conn->prepare("INSERT INTO image (titre, selogon, description, img_blob) VALUES (?, ?, ?, ?)"); // prepare statement 

   // bind params
   $stmt->bindParam(1, $img_titre);
   $stmt->bindParam(2, $selogon);
   $stmt->bindParam(3, $description);  
   // this is important I will explain it below after the code
   $stmt->bindParam(4, $fp, PDO::PARAM_LOB); 
   $stmt->execute();
   // if you want to check if it was inserted use affected rows from PDO
}
catch(PDOException $e)
{
   'Error : ' .$e->getMessage();
}

The most important thing is bind the file pointer ($fp) to PDO param called LOB which stands for Large Object

PDO::PARAM_LOB tells PDO to map the data as a stream, so that you can manipulate it using the PHP Streams API.

http://php.net/manual/en/pdo.lobs.php

After that you can use power of PHP streams and save it as binary safe stream. Reading the from streams make more sense but if I were you I'd really think if you want to save pictures directly in db, this doesn't seem to be a good idea.

MYSQLI solution:

If you don't use PDO but for example mysqli then streams are good idea as well but the solution is different.

The best option is to check SHOW VARIABLES LIKE 'max_allowed_packet'; this will print what is max allowed package size, if your image is bigger the blob will be corrupted.

To make it work you'll need to send data in smaller chunks using fread() + loop + feof() and mysqli function send_long_data

Example from php.net site: you can adjust it to your needs quite similar as I did above, the difference is that params are bound in different way.

$stmt = $mysqli->prepare("INSERT INTO messages (message) VALUES (?)");
$null = NULL;
$stmt->bind_param("b", $null);
$fp = fopen($_FILES['fic']['tmp_name'], "r");
while (!feof($fp)) {
    $stmt->send_long_data(0, fread($fp, 8192));
}
fclose($fp);
$stmt->execute();

Upvotes: 1

Related Questions