Blobbing Data With PHP and MySQL - Adding blobs to the database
(Page 3 of 7 )
Now that we've got our table structure sorted out, let's use a simple PHP script to capture files from a users computer and store them in our database. Create a new PHP script named getfiles.php and save it into a directory that your web server can process. Enter the following code into getfiles.php:
<html>
<head>
<title> Upload a File </title>
</head>
<body bgcolor="#FFFFFF">
<form enctype="multipart/form-data" name="frmUploadFile" action="grabfile.php" method="post">
<a href="http://www.devarticles.com">
<img border="0" src="http://www.devarticles.com/dlogo.gif">
</a>
<table border="0" cellpadding="0" cellspacing="0" bordercolor="#111111" width="100%">
<tr>
<td width="100%" bgcolor="#FF9900" height="22" colspan="2">
<p style="margin-left: 10"><b><font face="Verdana" size="2" color="#FFFFFF">
Upload a File</font></b></td>
</tr>
<tr>
<td width="100%" bgcolor="#FFE3BB" colspan="2">
<p style="margin-left: 10; margin-right: 10"><font face="Verdana" size="2">
<br>Please select a file from your local computer to upload to our web server
for saving in our database. This file can be of any type you like. Once you
have chosen a file, please click on the "Upload this file" button below.
<br> </font></td>
</tr>
<tr>
<td width="15%" bgcolor="#FFE3BB">
<p style="margin-left: 10"><font face="Verdana" size="2">
File Description:</font></td>
<td width="85%" bgcolor="#FFE3BB">
<input type="text" name="strDesc" size="20" maxlength="50"></td>
</tr>
<tr>
<td width="15%" bgcolor="#FFE3BB">
<p style="margin-left: 10"><font face="Verdana" size="2">File Location:</font></td>
<td width="85%" bgcolor="#FFE3BB">
<font face="Verdana" size="2">
<input type="file" name="fileUpload" size="20"></font></td>
</tr>
<tr>
<td width="33%" bgcolor="#FFE3BB">
<p style="margin-left: 10"><font face="Verdana" size="2">
<br>
<br>
</font></td>
<td width="67%" bgcolor="#FFE3BB">
<font face="Verdana" size="2">
<input type="submit" value="Upload this file" name="cmdSubmit"></font></td>
</tr>
</table>
</form>
</body>
</html>In the page above, we've created a HTML form that allows us to send files to the server. It allows the user to choose a file to upload, and to also enter a description for that file. The only difference between the form above and any other HTML form, is that the enctype attribute of our form is set to "multipart/form-data":
<form enctype="multipart/form-data" name="frmUploadFile" action="grabfile.php" method="post">This tells the browser that we may be sending files from the client's machine to the server, so it should be ready to prepare the headers and stream the files to the server, amongst other things. Our getfile.php page looks like this:

Our form will post its results to grabfile.php, which will be responsible for adding the file to the database as a BLOB. Let's create that file now.
Next: The grabfile.php script >>
More MySQL Articles
More By Mitchell Harper