I have a great download script I have been using from: http://www.zubrag.com/scripts/download.php. It uses the following headers:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

My goal is to call this php download file, which will display some initial html information then begin the download... similar to 'your download should start momentarily, otherwise click the following link'. The problem is that when I place the html code before the download code in this file, the contents of the file are displayed by the browser in binary (rather than starting the file download).

Is it possible to do this, or am I way off base? Any suggestions will be greatly appreciated.


8 Years
Discussion Span
Last Post by jimmiller96

Instead of directly calling download.php?id=144, once you have the info, pass the id=144 to a go-between page, downloadprep.php, or whatever strikes your fancy, before invoking the download.php script:

<?php $id = $_GET['id']; ?>
<title>Your download will start momentarily...</title>
  function startDownload() {
    location.href='download.php?id=<?php echo $id; ?>';
<body onLoad="startDownload()">
<p>Here it comes!</p>
<p>...otherwise, click <a href="download.php?id=<?php echo $id; ?>">here</a>.</p>
... other stuff you want to display ...


My 2¢. Hope this helps. :)

Edited by Wraithmanilian: Forgot to account for the &quot;...or else click here&quot; link. :)


Doh! Didn't even think of that. Worked like a charm. Thanks for the quick response... and example!


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.