Home arrow MySQL arrow Page 6 - Blobbing Data With PHP and 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 / 343
February 05, 2002
  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

Blobbing Data With PHP and MySQL - The downloadfile.php script
(Page 6 of 7 )

As mentioned on the previous page, when we click on the "Download now" link, the downloadfile.php script will be called up, passing the id of the blob requested as the blobId query string value. Create a new file named downloadfile.php and enter the following code into it:


global $blobId;


die("Invalid blobId specified");

// Database connection variables

$dbServer = "localhost";

$dbDatabase = "myFiles";

$dbUser = "admin";

$dbPass = "password";

$sConn = mysql_connect($dbServer, $dbUser, $dbPass)

or die("Couldn't connect to database server");

$dConn = mysql_select_db($dbDatabase, $sConn)

or die("Couldn't connect to database $dbDatabase");

$dbQuery = "SELECT blobType, blobData ";

$dbQuery .= "FROM myBlobs ";

$dbQuery .= "WHERE blobId = $blobId";

$result = mysql_query($dbQuery) or die("Couldn't get file list");

if(mysql_num_rows($result) == 1)


$fileType = @mysql_result($result, 0, "blobType");

$fileContent = @mysql_result($result, 0, "blobData");

header("Content-type: $fileType");

echo $fileContent;




echo "Record doesn't exist.";



In the code above we use the query string variable $blobId to retrieve the blobType and blobData fields from our myBlobs database. If there was a row returned from our query, then we save its type and actual data to two variables, called $fileType and $fileContent respectively.

We use the header function to change the type of content that we are sending to the browser. The default content type is text/html, which the browser assumes is a web page. If we didn’t include a call to the header function, then when we output our binary data it will look like garbage in the browser. Lastly, we use the echo command to actually output the contents of our binary file to the browser, which handles the file as it sees fit.

Take a look at the screen shots below for how the script works with different file types:

For Images:

Displays the image in the browser

For HTML documents:

Displays the HTML as normal

For MS Word documents:

Displays the MS Word document in the browser
blog comments powered by Disqus

- 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 

Developer Shed Affiliates


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