View Full Version : Inserting image into database problem ??

04-03-2007, 09:30 AM
Hello forums !!
I used the following code to upload the image in the DB

$tmp_name = $_FILES['file']['tmp_name'];
$filesize = filesize($tmp_name);
$filename = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$contents = fread(fopen($tmp_name,'rb'),$filesize);
//echo $contents;
//$contents = base64_encode($contents);
$addFile = "INSERT INTO mantis_bug_file_table
//echo $add5;
$resultFile = mysql_query($addFile) or die(mysql_error());
when i run this code , apache got down and nothing worked.
when i removed the $contents from the query or encoded the $contents with base64_encode() then it went right.
What's the problem with reading the upload image and inserting it in the database or this is not the way of inserting image into database ?
Any Idea ? Please Help
Thanks in advance to all of you !

04-03-2007, 10:01 AM
have you set the column 'content' to a blob column?

04-03-2007, 10:24 AM
Anyway i got the solution
what i did ?
i just placed the addslashes() in the $contents and inserted.and it worked...
Now One more Question ?
How to retrive the image from database ?
Thanks in advance to all of you !

04-03-2007, 06:50 PM
You need to make another file to output the image so you can link to it on your main page with the <img src="image.php?id=123" /> tag.

04-04-2007, 10:40 AM
Try to use my code in inserting image in the database...


if(!isset($_FILES) && isset($HTTP_POST_FILES))

$error["n_pic"] = "An image was not found.";

$imagename = basename($_FILES['n_pic']['name']);

$error["imagename"] = "The name of the image was not found.";

if(empty($error)) {
$newimage = "Images/Articles/" . $imagename;
$result = @move_uploaded_file($_FILES['n_pic']['tmp_name'], $newimage);

$error["result"] = "There was an error moving the uploaded file.";
$n_pic = $newimage;

Just try it....Thats what I used in inserting image into db.