1. Computing

Storing User Submitted Data and Files in MySQL

By , About.com Guide

3 of 7

Add File Uploads
Now you know how to store user data in MySQL, so let's take it one step farther and learn how to upload a file for storage. First let's make our sample database:

 CREATE TABLE uploads (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, description CHAR(50), data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50) ); 

The first thing you should notice is a field called id that is set to AUTO_INCREMENT. What this data type means is that it will count up to assign each file a unique file ID starting at 1 and going to 9999 (since we specified 4 digits). You will also probably notice that our data field is called LONGBLOB. There are many types of BLOB as we have mentioned before. TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB are your options, but we set ours to LONGBLOB to allow for the largest possible files.

Next, we will create a form to allow the user to upload her file. This is just a simple form, obviously you could dress it up if you wanted:

 <form method="post" action="upload.php" enctype="multipart/form-data"> 
 Description:<br> 
 <input type="text" name="form_description" size="40"> 
 <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> 
 <br>File to upload:<br> 
 <input type="file" name="form_data" size="40"> 
 <p><input type="submit" name="submit" value="submit"> 
 </form> 

Be sure to take notice of the enctype, it is very important!

Related Video
Zip File 101
Share a File or Folder in Vista
  1. About.com
  2. Computing
  3. PHP / MySQL
  4. PHP with MySQL
  5. SQL Form - SQL File - File Upload

©2013 About.com. All rights reserved.