Home arrow MySQL arrow Page 2 - Blobbing Data With PHP and MySQL

Blobbing Data With PHP and MySQL

It seems that the latest and greatest use for databases is storing large amounts of binary data, known as BLOB's. These BLOB's can store just about any type of data imaginable, including MS Word documents, GIF/JPEG images, PDF files, MP3's, etc. In this article Mitchell shows us how to create a binary file repository using PHP and MySQL that can store and retrieve several different file types.

Author Info:
By: Mitchell Harper
Rating: 5 stars5 stars5 stars5 stars5 stars / 343
February 05, 2002
  1. · Blobbing Data With PHP and MySQL
  2. · Creating the database
  3. · Adding blobs to the database
  4. · The grabfile.php script
  5. · Displaying the files
  6. · The downloadfile.php script
  7. · Conclusion

print this article

Blobbing Data With PHP and MySQL - Creating the database
(Page 2 of 7 )

Our document repository will use one database containing one table to store its data. Using the MySQL console application (c:\mysql\bin\mysql.exe for Win32 or /usr/bin/mysql for *nix users), create our database and switch to it with the following commands:

create database myFiles;

use myFiles;

Now let's create the table that will actually store our BLOB's as part of our myFiles database:

create table myBlobs


blobId int auto_increment not null,

blobTitle varchar(50),

blobData longblob,

blobType varchar(50),

primary key(blobId),

unique id(blobId)


We now have one database named myFiles, which contains one table named myBlobs. The details of each field in the myBlobs table are shown below:
  • blobId: An integer that will provide us with a unique numerical identifier for each blob (ie 1, 2, 3, etc). It is incremented and tracked internally by MySQL whenever we add a new record.
  • blobTitle: A description of each blob in the table, for example "My word document about golf", or "Picture of the sky". Servers no real purpose, but will come in handy when we are viewing our files in through a web page later on.
  • blobData: A binary field that will hold the contents of each file that we wish to store in the database. In our table, we have used the longblob data type, which can hold up to 4,294,967,295 characters. Other binary field types include mediumblob (which can hold up to 16,777,215 bytes), blob (which can hold up to 65535 characters), and tinyblob (which can hold up to 255 characters).
  • blobType: As you will see shortly, each different file type (such as .doc, .gif, .pdf, etc) has its own unique content type. When a file is uploaded via a web browser to a web server (which we will look at shortly), the browser cleverly passes that files type as part of its headers. We will be storing this type in the table so that we can choose to view only specific types of files from our myBlobs table. Content types are simple strings. The content type for a MS word document is "application/msword", the content type for a GIF image is "image/gif", etc.

blog comments powered by Disqus

- MySQL and BLOBs
- Two Lessons in ASP and MySQL
- Lord Of The Strings Part 2
- Lord Of The Strings Part 1
- Importing Data into MySQL with Navicat
- Building a Sustainable Web Site
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- PhpED 3.2 More Features Than You Can Poke ...
- Creating An Online Photo Album with PHP and ...
- Creating An Online Photo Album with PHP and ...
- Security and Sessions in PHP
- Setup Your Personal Reminder System Using PHP
- Create a IP-Country Database Using PERL and ...
- Developing a Dynamic Document Search in PHP ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials