Force Download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Force Download

Post by SidewinderX »

Without using the header() function, how else can I force a download?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: Force Download

Post by Chris Corbyn »

SidewinderX wrote:Without using the header() function, how else can I force a download?
I'm wondering why you ask, but maybe the <meta http-equiv=" ... " ... /> tags will do it. Sounds like wonky design to me though ;)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Smells like a headers already sent workaround
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: Force Download

Post by SidewinderX »

d11wtq wrote:I'm wondering why you ask
Well I've made a download component for Joomla! and it is giving me a heck of a time with the download of pdf files (only pdf files). I checked php.net and there is a bunch of problems with using headers to download pdf's with IE and other stuff but my code works fine when its not integrated into Joomla! If I use something as simple as this in Joomla it does not work, but using it stand-alone it works fine.

Code: Select all

<?php
$file = "http://www.irs.gov/pub/irs-pdf/fw4.pdf";
header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary"); 
header("Content-Disposition: attachment; filename="".basename($file).""");
readfile("$file");
?>
So I was hoping, without having to dig through Joomla's core, there was a different way.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

What "does not work" ? Joomla probably already sent output to the browser at the time you tried to send headers.

BTW, I'm also aware of IE's issues with dynamically generated PDF's (and other content). It only crops up when running over SSL with the no-cache header set. A straight redirect to a temporary file on disk will fix that ;)
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post by SidewinderX »

When you download the pdf file when it is integrated into Joomla it says it says the file is corrupt (dont know the exact message off the top). If you download the file 10 times, at least 5 of the files will have differing file sizes.

But using the script above not integrated will download the file properly every time
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

SidewinderX wrote:When you download the pdf file when it is integrated into Joomla it says it says the file is corrupt (dont know the exact message off the top). If you download the file 10 times, at least 5 of the files will have differing file sizes.

But using the script above not integrated will download the file properly every time
Make sure you exit() after the PDF has been output. If You don't, Joomla will continue to display it's own output which will inadvertently finish up at the bototm of the PDF document, thus corrupting it.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

d11wtq wrote:BTW, I'm also aware of IE's issues with dynamically generated PDF's (and other content). It only crops up when running over SSL with the no-cache header set. A straight redirect to a temporary file on disk will fix that ;)
In case this crops up, it is my experience that this error on IE is not just for PDF's, but almost all generated files (Office files, CSV, TXT). I experienced this recently and had to adjust all cache-control settings that involved no-cache (cache-control and pragma). I know it is not entirely related to this topic, but it is still handy to know.

d11wtq | Agreed, it is all dynamically created content that is uncached which causes the fault
Post Reply