Home arrow MySQL arrow Page 3 - Blobbing Data With PHP and MySQL
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 / 339
February 05, 2002
TABLE OF CONTENTS:
  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
SEARCH DEVARTICLES

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 &quot;Upload this file&quot; button below.&nbsp;

&nbsp;<br>&nbsp;</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>

&nbsp;</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 getfile.php page in the browser

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.
blog comments powered by Disqus
MYSQL ARTICLES

- 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 
Support 

Developer Shed Affiliates

 




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