0

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>
2
  • 1
    Each URL can carry only one resource. It's either a ZIP file, or a HTML page. Not both. Commented Aug 31, 2012 at 17:24
  • possible duplicate of PHP won't show page and download Commented Aug 31, 2012 at 17:24

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.