...

View Full Version : Inserting image into database problem ??



PHPycho
04-03-2007, 10: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);
//fclose($tmp_name);
//echo $contents;
//$contents = base64_encode($contents);
$addFile = "INSERT INTO mantis_bug_file_table
(bug_id,filename,filesize,file_type,date_added,content)
VALUES
('$idbug','$filename','$filesize','$file_type',NOW(),'$contents')";
//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 !

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

PHPycho
04-03-2007, 11: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 !

Inigoesdr
04-03-2007, 07: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.

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

unset($imagename);

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

if(!isset($_FILES['n_pic']))
$error["n_pic"] = "An image was not found.";

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

if(empty($imagename))
$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);

if(empty($result))
$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum