Solved

Download BLOB data from MySQL via php

Posted on 2011-03-01
2
742 Views
Last Modified: 2012-05-11
I have a table that has the following columns:   recID,Name,Type,docData

docData is a BLOB type

Using Apache as my web server, my SQL as my database backend. I have the files getting uploaded into the database fine. I originally had it so the upload would place the file into a directory and place a reference into the SQL table. I know have decided to put the docData into a BLOB field. I have several reasons to do this.

My final task in the conversion is to have the ability to download this docData from a PHP script.

My original download script was the following

 
I am still trying to get used to PHP. I am unclear if this code can be reused to download BLOB data? I know I could probably save the BLOB to a temp file and then do the same script as I have but I would prefer not to do this.
$fname = $_GET['fname'];
    $path  = "./docData/".$fname;
    $fullPath = $path.$_GET['download_file'];
    $file = $path.$_GET['download_file'];
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);

Open in new window

0
Comment
Question by:pHOdAT
2 Comments
 
LVL 19

Expert Comment

by:Michael701
ID: 35014485
sure you can replace the
readfile($file)
with
echo $row['docData']

Ok i'll assume you're done the mysql connect and query to get the record's data.

and you'll have to change the basename($file) to either a fixed value, of something based on the $row['Name']
0
 
LVL 11

Accepted Solution

by:
Bruce Smith earned 500 total points
ID: 35014835
Check out this article as it explains how to retrieve BLOBs from a MySQL database via PHP.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_4194-Resizing-and-Displaying-Images-In-PHP-from-a-MySQL-Database.html

hope this helps...
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question