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.
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:
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: