PHP DevCenter

oreilly.comSafari Books Online.Conferences.

We've expanded our LAMP news coverage and improved our search! Search for all things LAMP across O'Reilly!

Search
Search Tips

advertisement

Listen Print Subscribe to PHP Subscribe to Newsletters

Uploading, Saving and Downloading Binary Data in a MySQL Database

by Joao Prado Maia
09/15/2000

This article is aimed at answering one of the most asked questions on the PHP mailing list and discussion forums alike: How to store binary files in a MySQL database.

I ran into this same question when asked by a possible employer testing my programming skills to create a set of scripts to upload files to a MySQL database, download files from it, and also show an image, if the file was indeed an image. Anyway, I couldn't find any articles on how to do that, so I searched a lot in the PHP mailing lists to find my answer. This article is my way of "giving back" to the community. :)

I split the article into three pages,

  • Setting up the database
  • Creating the 'upload' scripts
  • Creating the 'download' script

so everything can be explained at the correct time, and also because I added some extra features, like a download script to get the binary files back from the database. This is extremely useful for downloading files of different types from a company database, or even displaying images stored in BLOB fields.

Setting up the database

I give instructions below for MySQL databases, since it was the database I used when developing my web application.

Before starting out, I need to explain what a BLOB field is. Like a teacher would say: "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 set up our database, we should optimize the fields on our tables to not waste any resources. This means that you shouldn't use a LONGBLOB field when you only need to upload 1.5 Kb files. Quoting from the MySQL Online Documentation:

  • TINYBLOB - A BLOB column with a maximum length of 255 (28 - 1) characters.
  • BLOB - A BLOB column with a maximum length of 65,535 (216 - 1) characters.
  • MEDIUMBLOB - A BLOB column with a maximum length of 16,777,215 (224 - 1) characters.
  • LONGBLOB - A BLOB column with a maximum length of 4,294,967,295 (232 - 1) characters.

For most applications, a MEDIUMBLOB field is more than enough, since it can hold up to 15 megs of binary data. Anyway, let's create the database and tables for our web application. We need to connect to the MySQL server:

mysql -u root -p
Enter password: ******

If the server is running on another host, use this:

mysql -u root -h hostname -p
Enter password: ******

Now to create the actual database:

mysql> CREATE DATABASE binary_files;
Query OK, 1 row affected (0.00 sec)

Ok, we now have a database to play with. We can create the tables now.

mysql> CREATE TABLE tbl_Files (
     > id_files tinyint(3) unsigned NOT NULL auto_increment,
     > bin_data longblob NOT NULL,
     > description tinytext NOT NULL,
     > filename varchar(50) NOT NULL,
     > filesize varchar(50) NOT NULL,
     > filetype varchar(50) NOT NULL,
     > PRIMARY KEY (id_files)
     > );

Now we need to create a custom user for this database/application for maximum security:

mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, ALTER
     > ON binary_files.*
     > TO binary_user@localhost
     > IDENTIFIED BY 'binary_password';

That's all. Now we can connect to the server from Apache/PHP using the user binary_user and password binary_password. Don't believe me? Try it yourself!

Next page: Creating the upload scripts

Pages: 1, 2, 3

Next Pagearrow




Recommended for You

  1. Cover of Learning PHP & MySQL
    Learning PHP & MySQL
    Print: $29.99
    Ebook: $23.99
  2. Cover of PHP Black Book
    PHP Black Book
    Print: $59.99
  3. Cover of Mastering Regular Expressions
    Mastering Regular Expressions
    Print: $44.99
    Ebook: $35.99
  4. Cover of PHP Cookbook
    PHP Cookbook
    Print: $44.99
    Ebook: $35.99

Tagged Articles

Post to del.icio.us

This article has been tagged:

mysql

Articles that share the tag mysql:

MySQL FULLTEXT Searching (155 tags)

Live Backups of MySQL Using Replication (152 tags)

Advanced MySQL Replication Techniques (125 tags)

Ten MySQL Best Practices (59 tags)

Rolling with Ruby on Rails (56 tags)

View All

php

Articles that share the tag php:

Understanding MVC in PHP (477 tags)

The PHP Scalability Myth (123 tags)

The Dynamic Duo of PEAR::DB and Smarty (53 tags)

PHP Form Handling (43 tags)

Very Dynamic Web Interfaces (39 tags)

View All

webdev

Articles that share the tag webdev:

Rolling with Ruby on Rails (351 tags)

Very Dynamic Web Interfaces (163 tags)

Understanding MVC in PHP (96 tags)

A Simpler Ajax Path (93 tags)

Ajax on Rails (61 tags)

View All

database

Articles that share the tag database:

MySQL FULLTEXT Searching (54 tags)

Live Backups of MySQL Using Replication (53 tags)

Advanced MySQL Replication Techniques (53 tags)

Dreaming of an Atom Store: A Database for the Web (49 tags)

How to Misuse SQL's FROM Clause (38 tags)

View All

blob

Articles that share the tag blob:

Uploading, Saving and Downloading Binary Data in a MySQL Database (2 tags)

View All

Sponsored Resources

  • Inside Lightroom
Advertisement

Sponsored by:

O'Reilly Media

©2010, O'Reilly Media, Inc.
(707) 827-7000 / (800) 998-9938
All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.
About O'Reilly
Academic Solutions
Authors
Contacts
Customer Service
Jobs
Newsletters
O'Reilly Labs
Press Room
Privacy Policy
RSS Feeds
Terms of Service
User Groups
Writing for O'Reilly
Content Archive
Business Technology
Computer Technology
Google
Microsoft
Mobile
Network
Operating System
Digital Photography
Programming
Software
Web
Web Design
More O'Reilly Sites
O'Reilly Radar
Ignite
Tools of Change for Publishing
Digital Media
Inside iPhone
makezine.com
craftzine.com
hackszine.com
perl.com
xml.com

Partner Sites
InsideRIA
java.net
O'Reilly Insights on Forbes.com