Storing Binary Data in MySQL Databases
Overview: A lot of people are curious to know how to add binary data such a Zip files, images etc in a MySQL Database, this articles shows you how to store binary data in a MySQL database and then how to reterive them back.
Binary data can be stored in a MySQL database in a BLOB field.
|
"A BLOB is a binary large object that can hold a variable amount of data." This essentially means that BLOB is a datatype that can hold binary content, and we can use it to store files. |
|
In order to understand the concept I will work with an example we will first create a table, then I will show you how to add (Binary Data) in it and then reterive it back.
Creating a Sample Table: We will name the table as 'binary_data_files' which will hold our Binary Data.
CREATE TABLE binary_data_files
(file_id tinyint(3) unsigned NOT NULL auto_increment,
bin_data mediumblob NOT NULL,
description tinytext NOT NULL,
filename varchar(50) NOT NULL,
filesize varchar(50) NOT NULL,
filetype varchar(50) NOT NULL,
PRIMARY KEY (file_id));
Now that we have a table I made a HTML Form, from where we can upload Binary Data (binary_form.htm)
Okay now that we have added data to the database, we will write a programm that shows the data in a table and another programme that retrives it.
We have two programms the first view_data.php and the second download_binary_data.php the first programme view_data.php is a very simple programme that lists the details of the entire table where our data is stored in a neat table.
File Name |
Description |
File size |
File type |
" . $rs[2] ." | ";
echo "". $rs[1] ." | "; //description
echo "". ($rs[3]/1024) ." KB | "; //filesize
echo "". $rs[4] ." | "; //filetype
}
?>
* Click on the file name to download the file
download_binary_data.php which downloads the actual data.
Home | Privacy Policy | Contact Us | Terms of Service
(c) 2002 - 2018 www.PHPbuddy.com Unauthorized reproduction/replication of any part of this site is prohibited.
|