TopCat
04-15-2002, 05:23 PM
This is the first part of a two part series. The second part is entitled Retrieving and Displaying Binary Image Files From MySQL (http://forum.100megswebhosting.com/showthread.php?s=&threadid=2972).
Objective
To upload the raw data of three (3) images of differing sizes for each of our products to MySQL database.
1. Create Table In Database
The three sizes are:
<ol type="1">
<li>Full Image - fields are:</li>
<ul>
<li>full_image</li>
<li>fi_filename</li>
<li>fi_filesize</li>
<li>fi_filetype</li>
</ul>
<li>Catalogue Image</li>
<ul>
<li>cat_image</li>
<li>cat_filename</li>
<li>cat_filesize</li>
<li>cat_filetype</li>
</ul>
<li>Thumb-nail Image</li>
<ul>
<li>tn_image</li>
<li>tn_filename</li>
<li>tn_filesize</li>
<li>tn_filetype</li>
</ul>
</ol>
We also need a field for the product id and perhaps an auto-incremented id field for indexing and to be able to refer to each individual image record.
The following is the code to create the table:
CREATE TABLE product_images (
prodimagesid bigint(20) unsigned NOT NULL auto_increment,
prod_id int(10) unsigned NOT NULL default '0',
full_image longblob,
fi_filename varchar(50) default NULL,
fi_filesize varchar(50) default NULL,
fi_filetype varchar(50) default NULL,
cat_image mediumblob,
cat_filename varchar(50) default NULL,
cat_filesize varchar(50) default NULL,
cat_filetype varchar(50) default NULL,
tn_image blob,
tn_filename varchar(50) default NULL,
tn_filesize varchar(50) default NULL,
tn_filetype varchar(50) default NULL,
PRIMARY KEY (prodimagesid)
) TYPE=MyISAM;
2. Upload Images
There are three php pages needed to upload the images. I have three images that you can download from my domain to use in this example if you so choose. Just point your browser to http://www.mylastoutpost.com/test/images and down load them to your hard disk and then use them in the example.
1. upload_image.php
There is actually no php code in this file, however, I usually use the php extension anyway. This is simply getting the data files from the hard drive that is needed for uploading.
<html>
<head>
<title>Upload Images</title>
</head>
<body>
<form method="POST" action="post_product_images.php"
name="product_images" enctype="multipart/form-data">
<table width="100%">
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Product ID<br>
<input type="text" name="prod_id" size="20">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Full image of product<br>
<input type="file" accept="jpg,jpeg" name="full_image" size="40">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Catalog image of product<br>
<input type="file" accept="jpg,jpeg" name="cat_image" size="40">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Thumb-nail image of product<br>
<input type="file" accept="jpg,jpeg" name="tn_image" size="40">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td align="center" valign="top">
<hr size="3"></hr>
Please be advised that if you are uploading images and
are using a<br>dial-up connection then the upload time may be
quite long.<br>
<hr size="3"></hr><br>
<input type="submit" value="Submit" name="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
There is one thing to keep in mind: enctype="multipart/form-data is mandatory and must be a part of the form tag.
2. post_product_images.php
This is the page that gets the data from the previous page that is needed for doing the actual work. Notice that there is an include file. This is the third and last file needed.
<?php
session_start();
include('inc_function_upload_image.php');
if(strlen($full_image) > 0)
{
$which_image = "full";
$form_data = $full_image;
$form_data_name = $full_image_name;
$form_data_size = $full_image_size;
$form_data_type = $full_image_type;
$result = upload_image($form_data, $form_data_name,
$form_data_size, $form_data_type,
(integer)$prod_id, $which_image);
if($result == "x")
{
echo "Could not create new record";
exit;
}
else
{
if(!$result)
{
echo "upload full failed<br>";
}
}
if(strlen($cat_image) > 0)
{
$which_image = "cat";
$form_data = $cat_image;
$form_data_name = $cat_image_name;
$form_data_size = $cat_image_size;
$form_data_type = $cat_image_type;
$result = upload_image($form_data, $form_data_name,
$form_data_size, $form_data_type,
(integer)$prod_id, $which_image);
if(!$result)
{
echo "upload cat failed<br>";
}
}
if(strlen($tn_image) > 0)
{
$which_image = "tn";
$form_data = $tn_image;
$form_data_name = $tn_image_name;
$form_data_size = $tn_image_size;
$form_data_type = $tn_image_type;
$result = upload_image($form_data, $form_data_name,
$form_data_size, $form_data_type,
(integer)prod_id, $which_image);
if(!$result)
{
echo "upload tn failed<br><br>";
}
}
?>
<html>
<head>
<title>Images Have Been Posted</title>
</head>
<body>
<center>
<br>
<p>
<font size="5" face="Comic Sans MS" color="#FF0000">
Product Images Have Been Posted.
</font>
<br>
</body>
</html>
3. inc_function_upload_image.php
This is the include file that actually stores the data into the database.
<?php
function upload_image($fd,$fd_name,$fd_size,$fd_type,$id,$which_image)
{
$fp = fopen($fd, "r"); //this opens the image file
$data = addslashes(fread($fp, filesize($fd))); //get the raw data
//open database
$db_conn=mysql_connect ("localhost", "user_name", "password");
mysql_select_db ("database");
//query the table to see if it already has product
$query = "select prod_id from product_images"
." where prod_id = $id";
$result = mysql_query($query, $db_conn);
if(!$result)
{
return "x";
}
/*
* if query is empty then insert a new image record
*/
if(mysql_num_rows($result) == 0)
{
$query = "INSERT INTO product_images "
."SET prod_id = $id";
$result = mysql_query($query, $db_conn);
}
/*
* After inserting a new record then update the record to
* include the necessary image information
*
* I'm formatting the query statement in such a way so that
* you can see what is going on.
*/
$query = "UPDATE product_images SET ";
switch($which_image)
{
case "full" :
$query = "$query "
."full_image = '$data', "
."fi_filename = '$fd_name', "
."fi_filesize = '$fd_size', "
."fi_filetype = '$fd_type' ";
break;
case "cat" :
$query = "$query "
."cat_image = '$data', "
."cat_filename = '$fd_name', "
."cat_filesize = '$fd_size', "
."cat_filetype = '$fd_type' ";
break;
case "tn" :
$query = "$query "
."tn_image = '$data', "
."tn_filename = '$fd_name', "
."tn_filesize = '$fd_size', "
."tn_filetype = '$fd_type' ";
break;
}
$query = $query."WHERE prod_id = $id";
$result = mysql_query($query, $db_conn);
if($result)
{
return -1;
}
else
{
return 0;
}
}
?>
__________________________________________________
This will get you started so have fun.
Remember, this is sample code and is not meant to be the end all. Use it as a starting point.
Go to the 2nd part: Retrieving and Displaying Binary Image Files From MySQL (http://forum.100megswebhosting.com/showthread.php?s=&threadid=2972).
TC
Objective
To upload the raw data of three (3) images of differing sizes for each of our products to MySQL database.
1. Create Table In Database
The three sizes are:
<ol type="1">
<li>Full Image - fields are:</li>
<ul>
<li>full_image</li>
<li>fi_filename</li>
<li>fi_filesize</li>
<li>fi_filetype</li>
</ul>
<li>Catalogue Image</li>
<ul>
<li>cat_image</li>
<li>cat_filename</li>
<li>cat_filesize</li>
<li>cat_filetype</li>
</ul>
<li>Thumb-nail Image</li>
<ul>
<li>tn_image</li>
<li>tn_filename</li>
<li>tn_filesize</li>
<li>tn_filetype</li>
</ul>
</ol>
We also need a field for the product id and perhaps an auto-incremented id field for indexing and to be able to refer to each individual image record.
The following is the code to create the table:
CREATE TABLE product_images (
prodimagesid bigint(20) unsigned NOT NULL auto_increment,
prod_id int(10) unsigned NOT NULL default '0',
full_image longblob,
fi_filename varchar(50) default NULL,
fi_filesize varchar(50) default NULL,
fi_filetype varchar(50) default NULL,
cat_image mediumblob,
cat_filename varchar(50) default NULL,
cat_filesize varchar(50) default NULL,
cat_filetype varchar(50) default NULL,
tn_image blob,
tn_filename varchar(50) default NULL,
tn_filesize varchar(50) default NULL,
tn_filetype varchar(50) default NULL,
PRIMARY KEY (prodimagesid)
) TYPE=MyISAM;
2. Upload Images
There are three php pages needed to upload the images. I have three images that you can download from my domain to use in this example if you so choose. Just point your browser to http://www.mylastoutpost.com/test/images and down load them to your hard disk and then use them in the example.
1. upload_image.php
There is actually no php code in this file, however, I usually use the php extension anyway. This is simply getting the data files from the hard drive that is needed for uploading.
<html>
<head>
<title>Upload Images</title>
</head>
<body>
<form method="POST" action="post_product_images.php"
name="product_images" enctype="multipart/form-data">
<table width="100%">
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Product ID<br>
<input type="text" name="prod_id" size="20">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Full image of product<br>
<input type="file" accept="jpg,jpeg" name="full_image" size="40">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Catalog image of product<br>
<input type="file" accept="jpg,jpeg" name="cat_image" size="40">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td width="65%" align="center">
Thumb-nail image of product<br>
<input type="file" accept="jpg,jpeg" name="tn_image" size="40">
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<tr>
<td align="center" valign="top">
<hr size="3"></hr>
Please be advised that if you are uploading images and
are using a<br>dial-up connection then the upload time may be
quite long.<br>
<hr size="3"></hr><br>
<input type="submit" value="Submit" name="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
There is one thing to keep in mind: enctype="multipart/form-data is mandatory and must be a part of the form tag.
2. post_product_images.php
This is the page that gets the data from the previous page that is needed for doing the actual work. Notice that there is an include file. This is the third and last file needed.
<?php
session_start();
include('inc_function_upload_image.php');
if(strlen($full_image) > 0)
{
$which_image = "full";
$form_data = $full_image;
$form_data_name = $full_image_name;
$form_data_size = $full_image_size;
$form_data_type = $full_image_type;
$result = upload_image($form_data, $form_data_name,
$form_data_size, $form_data_type,
(integer)$prod_id, $which_image);
if($result == "x")
{
echo "Could not create new record";
exit;
}
else
{
if(!$result)
{
echo "upload full failed<br>";
}
}
if(strlen($cat_image) > 0)
{
$which_image = "cat";
$form_data = $cat_image;
$form_data_name = $cat_image_name;
$form_data_size = $cat_image_size;
$form_data_type = $cat_image_type;
$result = upload_image($form_data, $form_data_name,
$form_data_size, $form_data_type,
(integer)$prod_id, $which_image);
if(!$result)
{
echo "upload cat failed<br>";
}
}
if(strlen($tn_image) > 0)
{
$which_image = "tn";
$form_data = $tn_image;
$form_data_name = $tn_image_name;
$form_data_size = $tn_image_size;
$form_data_type = $tn_image_type;
$result = upload_image($form_data, $form_data_name,
$form_data_size, $form_data_type,
(integer)prod_id, $which_image);
if(!$result)
{
echo "upload tn failed<br><br>";
}
}
?>
<html>
<head>
<title>Images Have Been Posted</title>
</head>
<body>
<center>
<br>
<p>
<font size="5" face="Comic Sans MS" color="#FF0000">
Product Images Have Been Posted.
</font>
<br>
</body>
</html>
3. inc_function_upload_image.php
This is the include file that actually stores the data into the database.
<?php
function upload_image($fd,$fd_name,$fd_size,$fd_type,$id,$which_image)
{
$fp = fopen($fd, "r"); //this opens the image file
$data = addslashes(fread($fp, filesize($fd))); //get the raw data
//open database
$db_conn=mysql_connect ("localhost", "user_name", "password");
mysql_select_db ("database");
//query the table to see if it already has product
$query = "select prod_id from product_images"
." where prod_id = $id";
$result = mysql_query($query, $db_conn);
if(!$result)
{
return "x";
}
/*
* if query is empty then insert a new image record
*/
if(mysql_num_rows($result) == 0)
{
$query = "INSERT INTO product_images "
."SET prod_id = $id";
$result = mysql_query($query, $db_conn);
}
/*
* After inserting a new record then update the record to
* include the necessary image information
*
* I'm formatting the query statement in such a way so that
* you can see what is going on.
*/
$query = "UPDATE product_images SET ";
switch($which_image)
{
case "full" :
$query = "$query "
."full_image = '$data', "
."fi_filename = '$fd_name', "
."fi_filesize = '$fd_size', "
."fi_filetype = '$fd_type' ";
break;
case "cat" :
$query = "$query "
."cat_image = '$data', "
."cat_filename = '$fd_name', "
."cat_filesize = '$fd_size', "
."cat_filetype = '$fd_type' ";
break;
case "tn" :
$query = "$query "
."tn_image = '$data', "
."tn_filename = '$fd_name', "
."tn_filesize = '$fd_size', "
."tn_filetype = '$fd_type' ";
break;
}
$query = $query."WHERE prod_id = $id";
$result = mysql_query($query, $db_conn);
if($result)
{
return -1;
}
else
{
return 0;
}
}
?>
__________________________________________________
This will get you started so have fun.
Remember, this is sample code and is not meant to be the end all. Use it as a starting point.
Go to the 2nd part: Retrieving and Displaying Binary Image Files From MySQL (http://forum.100megswebhosting.com/showthread.php?s=&threadid=2972).
TC