Introduction
You may be asking yourself: “Why put files inside my database? Why not just put them on the
file-system?”.
In most cases, that is exactly what you should do. It’s simple, effective, and requires very little
effort on your part.
There are, however, some special circumstances that do require a little more complex tactics.
For example, when handling sensitive data, putting the files into a database gives you a little
more control over how the data is handled and who has access to it.
This article shows a simple way of putting files into a MySQL table, using PHP.
Before you start
To get through this smoothly, you should be familiar with the following:
* PHP Basics
* MySQL Basics
* How to interact with MySQL in PHP (using the mysqli extension).
* HTML Forms and how to handle POST data in PHP.
The battle plan
As with all programs, before we start writing we need to plan a little ahead. Just so we know
what we are going to write before we write it.
Before we start on the program, we need to design the database. This is not a complex design,
as we are not talking about creating some complex filing system. We only need a single table,
containing a BLOB field for our file and various other fields to store information on our file, such
as name, size, type.
Now then. The first phase of the program is getting the file from our users onto the server where
our PHP can interact with it. This is the simplest part of the process, requiring only a basic HTML
form.
The second phase involves reading the uploaded file, making sure it was uploaded successfully and
adding it to the database. This is a similar process as the one used when uploading a file to the
file-system, but using the MySQL functions rather than the file-system functions.
The third phase is to list all files that have been uploaded and saved on the database, with a link
so it can be downloaded. The only problem here would be the fact that the file does not exists on
the server, so how do we create a link to it? That is a problem handled by phase 4, all we need to
do in phase 3 is create a link with the ID of the file to be downloaded embedded in the URL.
The fourth, and final, part is the one that is most confusing about this process. The part where we
fetch the file and send it to the client’s browser.
We start by using the MySQL functions, and the ID sent by phase 3, to fetch the file data from the database. Then we set a few headers, letting the browser know what to expect, before finally
sending the contents of the file.
Now, using this summary as a guide, lets start writing our program.
Phase 0: Building a database
The database is simple. One table with a BLOB field for the file data and a few fields for various
pieces of information relating to the file:
CREATE TABLE FileStorage ( FileID Int Unsigned Not Null Auto_Increment, FileName VarChar(255) Not Null Default 'Untitled.txt', FileMime VarChar(50) Not Null Default 'text/plain', FileSize BigInt Unsigned Not Null Default 0, FileData MediumBlob Not Null, Created DateTime Not Null, PRIMARY KEY (FileID) )
As you see, we store the file name, including the extension.
We have the mime type, which we use to let the browser know what kind of file we are dealing with.
The size of the file in bytes.
And finally the data itself, in a MediumBlob field.
Phase 1: Uploading the file
Now, we need to get the file from the user. The table we designed does not require any additional information from the user, so we will make this simple and create a HTML form with only a single
“file” input field and a submit button:
<form action="http://blog.950buy.com/add_file.php" method="post" enctype="multipart/form-data"> <input type="file" name="uploaded_file" /><br /> <input type="submit" value="Upload file" /> </form> <p> <a href="http://blog.950buy.com/list_files.php">See all files</a> </p>
Note the third attribute of the

No Responses to “Upload files into MySQL using php”