Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I display a Thankyou page after user downloads file?

Tags:

html

php

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???

like image 878
Mark Sowards Avatar asked Feb 04 '13 20:02

Mark Sowards


1 Answers

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.

Example:

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);

Update

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.

like image 110
thordarson Avatar answered Nov 13 '22 19:11

thordarson