Page 1 of 1

Force Download

Posted: Sun Apr 15, 2007 2:31 pm
by SidewinderX
Without using the header() function, how else can I force a download?

Re: Force Download

Posted: Sun Apr 15, 2007 2:59 pm
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 ;)

Posted: Sun Apr 15, 2007 3:25 pm
by John Cartwright
Smells like a headers already sent workaround

Re: Force Download

Posted: Sun Apr 15, 2007 4:51 pm
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.

Posted: Sun Apr 15, 2007 5:18 pm
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 ;)

Posted: Sun Apr 15, 2007 5:22 pm
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

Posted: Mon Apr 16, 2007 1:14 pm
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.

Posted: Mon Apr 16, 2007 1:46 pm
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