0

I have to send an image with php to database. When i send the image from the following code, the image is not displayed as you can see here

If I insert the image from the Insert function of phpmyadmin here , the image is displayed correctly.

Could you please help me?

Here is my code:

    <?php

require_once('connection.php');

if($_SERVER["REQUEST_METHOD"] == "POST") {

$myCreateStatement = "CREATE TABLE Temp (
                                 id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
                                 Immagine LONGBLOB NULL,
                                 nome VARCHAR(255) NULL,
                                 tipo VARCHAR(255) NULL
                                 )";
$conn->exec($myCreateStatement);

if((!isset($_FILES["file_inviato"])) || ($_FILES["file_inviato"]["error"] != UPLOAD_ERR_OK))
   echo "Error";
  
$tmp_name = $_FILES["file_inviato"]["tmp_name"];
$file_name = $_FILES["file_inviato"]["name"];
$file_type = $_FILES["file_inviato"]["type"];

$dati_file = file_get_contents($tmp_name);

$dati_file = addslashes($dati_file);

$my_Insert_Statement = $conn->prepare("INSERT INTO Temp (id, Immagine, nome, tipo) VALUES (1, :dati_file, :file_name, :file_type)");
$my_Insert_Statement->bindParam(':dati_file', $dati_file);
$my_Insert_Statement->bindParam(':file_name', $file_name);
$my_Insert_Statement->bindParam(':file_type', $file_type);
if ($my_Insert_Statement->execute()) {
    echo "Image sent";
} else {
    echo "Error";
}

$query = "SELECT * FROM Temp WHERE id = 1";
$stmt = $conn->prepare($query);
$stmt->execute();
                
$num = $stmt->rowCount();
if ($num) {
    $row = $stmt->fetch(PDO::FETCH_ASSOC);
    header("Content-type: image/jpeg");
    print $row['Immagine'];
    exit;
} else {
    echo "No image found";
}

}
?>


<html>
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 
<title>Storage file into DB</title> 
</head>
<body>
<p>Select a file</p> 
<form name="upload" enctype="multipart/form-data" method="post" action="prova_immagine.php"> 
<p>
<input type="file" name="file_inviato"><br>
<input type="submit" name="invia" value="Invia file">
</p> 
</form>
</body>
</html>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Does this answer your question? [PHP upload image](https://stackoverflow.com/questions/13465929/php-upload-image) – manqlele Jun 25 '21 at 12:30
  • 3
    `$dati_file = addslashes($dati_file);` probably messes up your image data. What was the purpose of putting that there? Are you not aware that your data doesn’t need any kind of “escaping” any more, when you are using prepared statements correctly (with placeholders & parameter binding)? – CBroe Jun 25 '21 at 12:30
  • I removed that line but nothing changed – Vincenzo Trisolini Jun 25 '21 at 12:55

1 Answers1

0

This line

$dati_file = addslashes($dati_file);

Shouldn't be required and is almost certainly messing up the data content of your file. Remove it and try again.

EDIT

Try outputting the image data as a base64 encoded image like

echo '<img src="data:image/jpeg;base64,'.base64_encode($row['Immagine']).'"/>';
DevWithZachary
  • 3,545
  • 11
  • 49
  • 101