RHF
RHF

Reputation: 1

php header Content-disposition

I have a very basic question illustrated by the code snippet below. This is the relevant part of a much bigger program in which I wish to download a zip file. The code as shown, with the comments in place, produce the expected browser output "Download the file now."

When I un-comment the code, the zip file is correctly downloaded to my browser!

However, the browser output is not produced. How do I regain control? I would like for the user to then have other options. (By the way, un-commenting the single "Content-disposition" line is sufficient to cause the loss of control.)

I have tried including the code, putting it in a function, many possible combinations of ob_start, ob_end flush, etc., all to no avail. I am sure I am overlooking something very fundamental and would appreciate some suggestions.

Thanks.

<?php
$sZipFN = 'file.zip';
// header("Content-type: application/zip");
// header("Content-disposition: attachment; filename=$sZipFN");
// readfile($sZipFN);
$sMsg = "Download the file now.";        
?>
<html>
  <body>
    <p> <?php echo $sMsg; ?> </p>
  </body>
</html>

Upvotes: 0

Views: 1048

Answers (1)

msEmmaMays
msEmmaMays

Reputation: 1063

You can't - you have to redirect to the options page and then start the download via javascript redirect (document.location = 'http://download.url/';)

This will start the download and leave the user on the options page like you desire.

Don't forget to include a 'Click here if the download fails to start' link somewhere near the top of the page (just in case javascript is disabled)

Upvotes: 1

Related Questions