I’m trying to show a HTML page with PHP right before it downloads a file. I understand that I can’t redirect to a different page and download a file at the same time, but why wouldn’t this work?
echo "<html>...Example web page...</html>";
$download = '../example.zip'; //this is a protected file
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename=example.zip');
readfile($download);
The file downloads, but it never shows the HTML page that is echoed. But if I remove the download, the page shows.
Because you cannot output anything before pushing custom headers out, I would suggest using JS to redirect to the download, which usually keeps you on the same page (as long as you’re just disposing the zip content and nothing else).
So, try this:
Or if you need a timer on it:
EDIT:
If you want to hide the file path, I would suggest making a download script, which JS will redirect to.
So basically, do exactly what you’re doing, then use JS to point to it. Like this:
Download.php:
And then in your main HTML file, use JS to point to it, with the correct ID: