Redirect after force download

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gkjr1
Forum Newbie
Posts: 3
Joined: Wed Apr 13, 2011 12:24 pm

Redirect after force download

Post by gkjr1 »

I have a force download, which uses headers obviously so I can't redirect with a header in the script. It pops the download up on the page, but I need the page to either refresh or redirect after download has started. Ive tried putting headers in the form for the download, tried in the force download in the code regardless, tried java and a header in the force download code, but am stuck. Any ideas?
gkjr1
Forum Newbie
Posts: 3
Joined: Wed Apr 13, 2011 12:24 pm

Re: Redirect after force download

Post by gkjr1 »

Sorry for this, im just kinda stuck here. Still have nothing working...
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Redirect after force download

Post by McInfo »

Do the reverse. Have the confirmation page trigger the download. For example, suppose a visitor requests confirm.php.

confirm.php

Code: Select all

<!DOCTYPE html>
<html>
    <head>
        <title>Confirmation</title>
        <meta http-equiv="refresh" content="0;url=http://localhost/download.php" />
    </head>
    <body>
        <p>Your download should have begun. If not, try <a href="http://localhost/download.php">download.php</a>.</p>
    </body>
</html>
The meta tag tells the visitor's browser to request download.php.

download.php

Code: Select all

<?php
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename=example.txt');
echo 'Example';
Because download.php specifies that it should be downloaded as an attachment, the visitor's browser stays on confirm.php while downloading the file.
gkjr1
Forum Newbie
Posts: 3
Joined: Wed Apr 13, 2011 12:24 pm

Re: Redirect after force download

Post by gkjr1 »

mmm thanks! Can't believe that logic slipped my mind.
Post Reply