php mysql help--retrieving an image from database
Posted: Sun Oct 17, 2010 5:27 am
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:
code for the get.php
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:
Code: Select all
<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>Code: Select all
<?php
// conecting to database
mysql_connect("localhost","root","") or die("cant connect to server.");
mysql_select_db("databaseimage") or die("cant connect to database");
//echo $id = addslashes($_REQUEST['id']);
//echo $image = mysql_query("SELECT * FROM store WHERE id = $id");
//echo $image = mysql_fetch_assoc($image);
//echo $image = $image['image'];
$result = mysql_query("$insert");
header("Content-type: image/jpeg");
echo mysql_result($result, 0);
mysql_close($link);
//header("Content-length: $size");
//header("Content-type: image/jpeg");
//header("Content-Disposition: attachment; filename=$name");
//echo $image;
//exit ();
?>