Solved

Download BLOB data from MySQL via php

Posted on 2011-03-01
2
737 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
Comment Utility
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:
patsmitty earned 500 total points
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now