By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,058 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,058 IT Pros & Developers. It's quick & easy.

Download Then Redirect

P: 27
I wonder if anyone can help me out with this. I am sending the user to a download page where when they click on the download button the file is downloaded and then I want the user redirected after the file download to a confirmation page.

My download processing page is as follows:

[PHP]<?php

$mytalk = $_GET['varrqust'];

if($_GET['varrqust'] == "1"):
$mytalk = 'Talk 1';
$talksize = 3Mb';
$talkpath = 'talk1.pdf';

elseif($_GET['varrqust'] == "2"):
$mytalk = 'Talk 2';
$talksize = '3Mb';
$talkpath = 'talk2.pdf';

elseif($_GET['varrqust'] == "3"):
$mytalk = 'Talk 3';
$talksize = '3Mb';
$talkpath = 'talk3.pdf';

endif;

$path = $talkpath;
$file = basename($path);
$size = filesize($path);

//do download
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-Transfer-Encoding: binary");
header("Content-Length: $size");

@readfile($path);

header("Location: sbx_dwnldthx.php?varrqust=$varrqust"); /* Redirect browser */

exit();

?>[/PHP]

In this instance the file is downloaded as expected but the user doesn't get redirected.

If I change the order of the last few lines of code as follows:

[PHP]<?php

..........

header("Location: sbx_dwnldthx.php?varrqust=$varrqust"); /* Redirect browser */

@readfile($path);

exit();

?>[/PHP]

The user gets redirected as required but the file doesn't get downloaded!

Can someone please explain where I am going wrong.

Thanks in advance.
Oct 11 '07 #1
Share this Question
Share on Google+
4 Replies


P: 6
You could use some JavaScript to open the download file in a popup window then have the 'Thanks!' page on the current page, although I don't think this is the appropriate section, the following code might help:

[html]
<script type="text/javascript">
function download() {
var w = window.open("http://path/to/download.ext", "", "width=100,height=100,scrollbars=no,status=no" );
}

function thanks() {
document.getElementById('thanks').style.display = 'block';
}
</script>

<input type="button" onclick="download(); thanks(); " value="Download file" />

<div id="thanks" style="display: none">
your thanks message here
</div>

[/html]
Oct 11 '07 #2

P: 27
Thanks for your reply, t0m66. Is there any way to achieve the redirection just using PHP, as I would prefer to avoid generating pop-up pages if possible?

Thanks again.
Oct 11 '07 #3

P: 6
Thanks for your reply, t0m66. Is there any way to achieve the redirection just using PHP, as I would prefer to avoid generating pop-up pages if possible?

Thanks again.
I don't know of one, although there is the possibility of using a Location: header to redirect them to the download, although I don't think you could put some 'Thanks' message there too.
Oct 11 '07 #4

P: 27
I'm trying to use

header ("Location: - see initial code

as it is without any apparet success. It is almost as if as soon as I use it either wants to stop everthing else working or it is prevented from running by the other code purely depending upon its position! And doesn't appear to be any happy medium.

Thanks again for your input.
Oct 11 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.