Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > PHP > upload image in mysql database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-16-04, 13:54
faamugol faamugol is offline
Registered User
 
Join Date: Mar 2004
Posts: 55
upload image in mysql database

Hello gys,

I use the following code to store image in mysql databse..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
if($_POST)
{
if(!is_array($_FILES) || !is_array($_FILES['visitor_photo']))
{
die("no file was uploaded");
}
$tmp = $_FILES['visitor_photo']['tmp_name'];
$fp = fopen($tmp, 'rb');
$image = fread($fp, filesize($tmp));
fclose($fp);
$name = $_POST['visitor_name'];
$db = @mysql_connect("localhost", "myusername", "mypassword") or die (mysql_error());
@mysql_select_db("competence", $db) or die (mysql_error());

// gzip file
//$image = gzdeflate($image, 9);
$image = mysql_real_escape_string($image, $db);
$result = @mysql_query("INSERT INTO `tb_member` (`nom`, `photo`) VALUES ('$name' ,'$image')", $db);

if($result === false)
{
die("could not insert into database");
}
unlink($tmp);
exit("upload successful");
}
?>
<form action="" method="post" enctype="multipart/form-data" name="fjoindre" id="fjoindre">
Name:
<input type="text" name="visitor_name">
Image:
<input name="visitor_photo" type="file" id="visitor_photo">
<input type="submit" value="Send" name="Submit">
</form>
</body>
</html>

but I just need to know what should the type of the field Photo be in the database.

I have defined the table tb_member as:
v_ib : int not null
nom: varchar(40) not null
photo: longtext not null


It seems to work but how can I get this image from my database to show it on my pages.

Thank in advance for all your answers..
Reply With Quote
  #2 (permalink)  
Old 10-17-04, 14:00
savbill savbill is offline
Registered User
 
Join Date: Feb 2004
Posts: 525
Most of the programs I've used for image management just store the image name and path in the database and the image is located in a directory on your server. I think it would be slower to extract the image from the database and then display vs. using print or echo to write the path/name into the html output.


/
__________________
~

Bill
Reply With Quote
  #3 (permalink)  
Old 11-03-04, 15:39
Dzilly Dzilly is offline
Registered User
 
Join Date: Nov 2004
Posts: 1
This is a method I use to show images stored in a MySQL db using PHP

$type is something like 'image/jpeg'
$name is just the name of the image
$image_data is the binary data of the image

header('Content-type: $type);
header("Content-Disposition: attachment; filename='$name'");
header("Content-Description: PHP Generated Data");
echo $image_data;

This works fine for me, hope you can use it...
Reply With Quote
  #4 (permalink)  
Old 11-16-04, 16:27
nitrog7 nitrog7 is offline
Registered User
 
Join Date: Nov 2004
Posts: 1
Blob

I believe the type of field is a "blob"

-G
Reply With Quote
  #5 (permalink)  
Old 11-28-04, 09:44
sstoyan sstoyan is offline
Registered User
 
Join Date: Nov 2004
Posts: 2
Post

straight from the mysql docs the types for blobs are

TINYBLOB
TINYTEXT
A BLOB or TEXT column with a maximum length of 255 (2^8 - 1) characters.

BLOB
TEXT
A BLOB or TEXT column with a maximum length of 65535 (2^16 - 1) characters

MEDIUMBLOB
MEDIUMTEXT
A BLOB or TEXT column with a maximum length of 16777215 (2^24 - 1) characters

LONGBLOB
LONGTEXT
A BLOB or TEXT column with a maximum length of 4294967295 or 4G (2^32 - 1) characters






---------------------
dev @
www.viewmoresoft.com
tools for your database
Reply With Quote
  #6 (permalink)  
Old 11-29-04, 07:26
Nanaki Nanaki is offline
Registered User
 
Join Date: Nov 2004
Location: Norway
Posts: 4
Not trying to hijack, but this is just what I'm looking for..

I posted the (almost) exact same question some days ago (just below this post), and I'll try the code you posted Dzilly
Code:
$type is something like 'image/jpeg' $name is just the name of the image $image_data is the binary data of the image header('Content-type: $type); header("Content-Disposition: attachment; filename='$name'"); header("Content-Description: PHP Generated Data"); echo $image_data;


Hope it works.
Do you output is as : image?id=(id from db) ?

And if you'll have to output some text as well, how would you alter the code?
Just add "echo $myTextVariable" ?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On