I am using this little pusher php file to download an 'exe' file for the user to save when they fill-out a form.
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
with this method there is no link to the online $file location for someone to follow but for some reason I cannot display a thank you page after or even immediately before the code is executed.
I've tried header( "Location: $thankyouurl" ); immediately after the above code, but nothing is ever displayed and I've tried echoing the html source of the thank you page just prior to running the above code, but that causes the exe file to be downloaded to the web page actually crashing the browser.
Any suggestions???
Point the user directly to a static page that displays your thank you message. Then, use JavaScript to set the window.location
to your download script.
If your download page's headers are set up correctly, the browser will start downloading the file, while your user is reading your thank you message.
Make sure to display a direct download link in case the user has JavaScript disabled.
index.php
<a href="thankyou.php">Click here to download.</a>
thankyou.php
<html>
<head>
<script type="text/javascript">
function startDownload() {
window.location = "/download.php";
}
</script>
</head>
<body onload="startDownload();">
<h1>Thank you!</h1>
<p>Your download will start in a moment. If it doesn't, use this <a href="download.php">direct link.</a></p>
</body>
download.php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
In order to pass data from index.php
on to the download script, you could use GET
variables. The link to the thankyou.php
page would become thankyou.php?download=12
where download is an ID for you to grab the correct file. You would then pass that variable on to your download script by echoing it out in your markup like so:
index.php
<a href="thankyou.php?download=12">Click here to download.</a>
thankyou.php
<html>
<head>
<script type="text/javascript">
function startDownload() {
window.location = "/download.php?file=<?=$_GET['download']?>";
}
</script>
</head>
<body onload="startDownload();">
<h1>Thank you!</h1>
<p>Your download will start in a moment. If it doesn't, use this <a href="download.php?file=<?=$_GET['download']?>">direct link.</a></p>
</body>
You'd then grab the value in your download script and handle it any way you like.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With