I've followed an online tutorial for storing an image into a MySQL database.
I have created a table (databaseimage) with three fields id (primary key, auto increment), name, image (mediumblob type).
The php is supposed to display the image after it has been uploaded but I get the problem uploading image from the if then else statement. Also the full image name including the name of the tmp dir is displayed but the php is only supposed to display the filename. Despite these issues I can see the images inside the database with the correct filenames and sizes!
PHP Code:
<html>
<head>
<title>Upload image</title>
</head>
<body>
<form action="imageupload.php" method="post" enctype="multipart/form-data">
File:
<input type="file" name="image"> <input type="submit" value="upload">
</form>
<?php
// connect to database
mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());
// file properties
echo $file = $_FILES['image']['tmp_name'];
if (!isset($file))
echo "Please select an image.";
else
{
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = addslashes($_FILES['image']['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 databaseimage VALUES ('','$image_name','$image')"))
echo "Problem uploading image";
else
{
$lastid = mysql_insert_id();
echo "Image uploaded<p />Your image:<p /><img src=get.php?id=$lastid>";
}
}
}
?>
</body>
</html>
PHP Code:
<?php
mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db('dbname') or die(mysql_error());
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM databaseimage WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>