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.


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. :)

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