create a download link in php to a file in mysql blob field

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jetson1
Forum Newbie
Posts: 1
Joined: Sat Jan 03, 2009 6:07 pm

create a download link in php to a file in mysql blob field

Post by jetson1 »

create a download link in php to a file in a mysql blob field

Hi everyone!
I'm new to the board and relatively new to php. I'm trying to setup my website to use php and mysql and I'm having a little trouble. I've got almost everything working at the moment except for creating download links. I want users, regular users from the internet who are not registered etc., to be able to download a sample file from my website. The sample file is a microsoft excel file 2003 or 2007; the file is stored in a blob type field in my mysql database. I've got the file uploaded and everything up to there working. The problem is that I am not sure how to create a download link to this excel file. I want it to be a dynamic download link, since there will be many excel files, which, when clicked, will open the download link to get the file.

So, my problem is that I don't know how to create this link.

I know that I need headers, something like this:

Code: Select all

header("Content-type: application/vnd.ms-excel");
header("Cache-Control: no-cacher, must-revalidate");
header("Expires: Sat, 13 May 1991 01:00:00 GMT");
and that I might also need something like this:

Code: Select all

<?php
 
// open the file in a binary mode
$name = '$row['excelfilel']';
$fp = fopen($name, 'rb');
 
 
 
// dump the picture and stop the script
fpassthru($fp);
exit;
 
?>

But, I'm really not sure how to make this work. Any help in creating this download link to an excel file contained in a blob field in mysql would be greatly appreciated. I'm sure this is probably really easy to do; thanks for your time!
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: create a download link in php to a file in mysql blob field

Post by novice4eva »

Put these two line of code at the top of a php page

Code: Select all

 
header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
 
followed by a echo command on your blob content. And on the link where you want to initiate the download, put the link of this file.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: create a download link in php to a file in mysql blob field

Post by VladSun »

novice4eva wrote:

Code: Select all

header('Content-type: application/vnd.ms-excel');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
PDF? :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: create a download link in php to a file in mysql blob field

Post by novice4eva »

Straight copy paste from MANUAL :mrgreen:
Correction

Code: Select all

 
header('Content-Disposition: attachment; filename="myXLS.xls"');
 
Post Reply