lundi 13 avril 2015

No image is shown in PHP from MySqL

I am trying to display an image from my php page by saving the image in the database [MySql] and when I retrieve it it did't show me the image. However, I can see the image when I click on it in MySql, but it is not shown in the php file.


Images Table: id INT


image BLOB


index.php



<html>
<head>
<title>Uploade an image</title>
</head>

<body>
<form action="test_image.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="Uploade">
</form>

<?php
mysql_connect("", "", "")
or die("<p>Error connecting to database: " . mysql_error() . "</p>");

mysql_select_db("test")
or die("<p>Error selecting the database: " . mysql_error() . "</p>");

// file propoerties
$file = $_FILES['image']['tmp_name'];

if(!isset($file))
echo "Please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_size = getimagesize($_FILES['image']['tmp_name']);

if($image_size == FALSE)
echo "That's not an image";
else
{
if(!$insert = mysql_query("INSERT INTO Images (image) VALUES ('$image')"))
echo "Problem uploading image.";
else
{
$lastid = mysql_insert_id();
echo "Image uploaded <p /> Your image: <p /> <img width='500' height='500' src=getimage.php?id=$lastid>";
}
}
}

?>

</body>
</html>


getimage.php



<?php
mysql_connect("", "", "")
or die("<p>Error connecting to database: " . mysql_error() . "</p>");

mysql_select_db("test")
or die("<p>Error selecting the database: " . mysql_error() . "</p>");

$id = addslashes($_REQUEST['id']);

$image = mysql_query("SELECT * FROM Images WHERE id = $id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];

header("Content-type: image/jpeg");

echo $image;
?>


I can upload the image but I can't retrieve it!!


Thank you,


Aucun commentaire:

Enregistrer un commentaire