Link to home
Start Free TrialLog in
Avatar of pHOdAT
pHOdATFlag for United States of America

asked on

Download BLOB data from MySQL via php

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

Avatar of Michael701
Michael701
Flag of United States of America image

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']
ASKER CERTIFIED SOLUTION
Avatar of Bruce Smith
Bruce Smith
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial