Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding pictures into mySQL

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Does anybody know of a good tutorial for uploading pictures into a database and showing the picture with the other query results?
 
Hi Mattmcg,
I haven't tested this yet, but here goes.
this is not the complete code ,but will give you
ideas.
I'm assume'n you know how to upload files using forms
and how to extract data from forms. If not
go to and read up.

------ add_image.php--------------
// first use a form data to upload files

// is it jpeg or gif?
$image_attrib = GetImageSize($uploaded_image);
$err_flag = 0;
if (($image_attrib[2] != 1 & $image_attrib[2] !=2){
// not jpeg or gif
$err_flag = 1;
}

if ($err_flag != 1){
// insert image
// open file
$image_file = addslashes(fread(fopen($uploaded_image,"r"),10000));

// you'll want to get the image type and store that too.
$SQL = "INSERT INTO $table
(image,image_type) VALUES
('$image_file','$image_type')";
mysql_query($SQL,$dbh);
}
-------------- end addimage script --------------------

-------------- view_image.php ------------------------
// script to view images.
// usages : <img src=&quot;view_image.php?pic_id=$pic_id&quot;>
$dbh = mysql_connect($host,$user,$password);
mysql_select_db($db_name,$dbh);

$SQL = &quot;SELECT image,image_type FROM $table
WHERE pic_id = '$pic_id'&quot;;
$result = mysql_query($SQL,$dbh);
$record = mysql_fetch_array($result);
$image = $record[image];
$image_type = $record[image_type];

// assum'n $image_type is either &quot;image/gif&quot; or
// &quot;image/jpeg&quot;

Header(&quot;Content-type: $image_type&quot;);
echo $image;
flush();

-------------------- end of script ---------------------

You'll probably want to do error check and stuff .. don't
just copy and paste .. this script was meant to get you
start in the right direction.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top