PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
hello there,
I am quite new to php....i am trying to develop this php page which uploads the image from the browser then it saves it in a mysql database. the data that is saved in database is saved as a mediumblob type but when I try to retrieve the image it doesnot shows an image instead a small box with a question mark in it.
this is my code:
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
File:
<input type="file" name="myfile">
<br>
<input type="submit" value="upload">
</form>
<?php
// conecting to database
mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");
// file properties
/* here we are declaring a variable which uses a builtin function of $_FILES and the first part of it is the name of the image, which we have selected in input name ="image" section. and the second part is the temporary name and location where the file is being uploaded, usually its the temp folder of the server....you can check it by writing echo before the $file */
echo "name: ";
echo $name = $_FILES['myfile']['name'];
echo "<BR>";
echo "type ";
echo $type = $_FILES['myfile']['type'];
echo "<BR>";
echo "Size: ";
echo $size = $_FILES['myfile']['size'];
echo "<BR>";
echo "temp: ";
echo $temp = $_FILES['myfile']['tmp_name'];
echo "<BR>";
echo "error code: ";
echo $error = $_FILES['myfile']['error'];
echo "<BR>";
echo "<BR>";
echo "file contents: "; // this will be used to save in database as BLOB (Binary Large Object) data type.
echo $file_contents = addslashes(file_get_contents($_FILES['myfile']['tmp_name']));
echo "<BR>";
// now inserting the data into database
if (!$insert = mysql_query("INSERT INTO store VALUES ('','$name','$file_contents')"))
echo "problem uploading file";
else
{
$last_id = mysql_insert_id();
echo "Image uploaded.";
echo "<BR>";
echo "<img src=get.php?id=$last_id>";
}
?>
</body>
</html>
<?php
// conecting to database
mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM store WHERE id = $id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-length: $size");
header("Content-type: image/jpeg");
echo $image;
?>
Is there a special reason why you want to save the image in the database? A more normal arrangement is to save an image in an 'images' directory, and to use the database to save information about the image, such as its size, location, name etc.
Storing BLOBs by putting the data directly inline with the query is risky. You're seeing one of the potential problems.
a) Use a prepared query and the 'b'lob parameter type. The mysql extension doesn't support it - you'll need another extension (like PDO or mysqli).
b) Do as cpetercarter suggested and store the images as files instead of in a database. I suggest it too.