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>