Force Download
Posted: Sun Apr 15, 2007 2:31 pm
Without using the header() function, how else can I force a download?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
I'm wondering why you ask, but maybe the <meta http-equiv=" ... " ... /> tags will do it. Sounds like wonky design to me thoughSidewinderX wrote:Without using the header() function, how else can I force a download?
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.d11wtq wrote:I'm wondering why you ask
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");
?>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.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
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 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