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
Solved

Download BLOB data from MySQL via php

Posted on 2011-03-01
2
743 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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

790 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