?
Solved

Download BLOB data from MySQL via php

Posted on 2011-03-01
2
Medium Priority
?
746 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
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.
Suggested Courses

770 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