Storing Images Into MySQL Database Table

Friday, June 8, 2007

First of all, I don't recommend it. Storing images data into MySQL database server consumes lot of server resources, especially when the size is large. Storing thumbails might be an exception, but IMHO uploading images physically is stil a better idea. The other problem with storing images data into MySQL database is exporting the data. You may find it's difficult with phpmyadmin to export the data and inserting it to a new MySQL table. I know I did. I'll give you a little illustration, try open a small image in your computer, but don't open it with your favorite image viewer software use notepad or other text-viewer instead. The unreadable chars appeared there is exactly the ones we're going to insert into MySQL database table when we store the images into database. Get my point? But if you can't resist creating an image database in MySQL, here's how. I assume you already had a working apache(or other webserver)-PHP-MySQL environment. This is just a quick-easy example, so maybe it's not well made in a programming manner. You will need 2 files, and a table in your database to do this operation. First, the file(and the form) to upload the image-data into database I'll name it image_upload.php. Second, file to extract the image-data from database and print the output to the browser, I'll name it image_view.php. And the last, a table to store the image information. Here's a simple query to create it.
CREATE TABLE images ( image_id smallint(4) NOT NULL auto_increment, image_binary blob NOT NULL, image_alt varchar(20) default NULL, image_type varchar(15) default NULL, PRIMARY KEY (image_id) ) TYPE=MyISAM;
And these are the codes for the php files image_upload.php
<?php echo "<form method=\"post\" action=\"\" enctype=\"multipart/form-data\">"; echo "<input name=\"image\" type=\"file\">"; echo "<input name=\"MAX_FILE_SIZE\" value=\"1000000\" type=\"hidden\">"; echo "<input name=\"submit\" value=\"submit\" type=\"submit\">"; echo "</form>"; if($submit){ //insert your database connection here $host=""; $user=""; $password=""; $dbname=""; $dbh=mysql_connect($host, $user, $password) or die(mysql_error()); mysql_select_db($dbname, $dbh) ; //end of database configuration $image_binary = addslashes(fread(fopen($image, "r"), filesize($image))); $query="INSERT INTO `images` (image_binary, image_alt, image_type) VALUES ( $image_binary, $image_name, $image_type)"; $res=mysql_query($query, $dbh) or die(mysql_error()); } @mysql_close(); ?>
image_view.php
<?php if($id){ //insert your database connection here $host=""; $user=""; $password=""; $dbname=""; $dbh=mysql_connect($host, $user, $password) or die(mysql_error()); mysql_select_db($dbname, $dbh) ; //end of database configuration $data=mysql_result($res, 0, "image_binary"); $type=mysql_result($res, 0, "image_type"); Header ("Content-type: $type"); echo $data; }; ?>
The 'id' in file image_view.php is the image_id on images table. So if you want the extract the image to a HTML page you can use <img src="http://www.blogger.com/image_view.php?id=2" /> instead of <img src="http://www.blogger.com/somefile.png" />. I didn't cross-check the script though, and I'm open for any corrections, suggestions, or anything.

0 comments: