user2494737
user2494737

Reputation: 449

PHP download/redirect

Okay so this is how I rewrote it. Any changes? Will this work? I've added the Javascript at the end with the timeout.

if (file_exists($file)) {
    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);
}
?> 
<script type="text/Javascript">
window.setInterval(function()
{
    window.location = "www.moneymovers.com";
}, 5000);
</script>:

Upvotes: 1

Views: 652

Answers (2)

Dany Caissy
Dany Caissy

Reputation: 3206

You can put the location header wherever you want, as long as nothing has been outputted (echo).

If you have outputted something, you can output something like this to redirect :

<script type="text/javascript">
    window.location = "www.example.com";
</script>

EDIT :

In your case, it is not possible to do what you're looking for, as far as I know. You will have to manage this case in the caller page. The Javascript will not be called because you've modified the Content-Disposition.

Upvotes: 1

Peter Geer
Peter Geer

Reputation: 1162

You can't download and then do a header redirect. The client is going to redirect as soon as it sees the header, which means the download won't work. But you also can't output the header after the download, because then the redirect won't happen. Either way, it's not gonna work.

I would recommend doing this at a different level. For example, when the user clicks the download link, have some JavaScript start the download in a new tab/window and then redirect to the desired location in the current tab.

Upvotes: 1

Related Questions