Serving files and redirecting using headers.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gooseman

    #1

    Serving files and redirecting using headers.

    Hi,

    I have a page where form data is passed to a php page (just script) to
    process the input. This page calls a function to output a file:

    function sendFileViaHead er($path, $name) {
    $fp = fopen($path . $name, 'r');
    header("Cache-Control: ");// leave blank to avoid IE errors
    header("Pragma: ");// leave blank to avoid IE errors
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"" . $name .
    "\"");
    header("Content-length: ".(string)(file size($path . $name)));
    fpassthru($fp);
    }

    The original script then calls a further header that will redirect the
    user page to a different (or updated original page):

    header('Locatio n: http://www.new.com');

    However, only the file is sent. The second header is never called. It
    is either one or the other. Never both. How do I get around this (I've
    tried buffering - ob_start(), ob_end_flush() to no avail)?

    Thanks!
  • Pedro Graca

    #2
    Re: Serving files and redirecting using headers.

    Gooseman wrote:[color=blue]
    > I have a page where form data is passed to a php page (just script) to
    > process the input. This page calls a function to output a file:
    >
    > function sendFileViaHead er($path, $name) {
    > $fp = fopen($path . $name, 'r');
    > header("Cache-Control: ");// leave blank to avoid IE errors
    > header("Pragma: ");// leave blank to avoid IE errors
    > header("Content-type: application/octet-stream");
    > header("Content-Disposition: attachment; filename=\"" . $name .
    > "\"");
    > header("Content-length: ".(string)(file size($path . $name)));
    > fpassthru($fp);
    > }
    >
    > The original script then calls a further header that will redirect the
    > user page to a different (or updated original page):
    >
    > header('Locatio n: http://www.new.com');
    >
    > However, only the file is sent. The second header is never called.[/color]

    headers sent after output are not recognized as headers by the browser
    (if they get sent at all). If you redirect before sending the file
    contents, that is what happens: the browser honors the redirect and
    never receives the file.
    [color=blue]
    > How do I get around this?[/color]

    Unless I miss something (like maybe JavaScript), you don't.

    Provide a link to download the file and another link for the 'redirect'.

    <a href="file_outp ut.php">downloa d the file</a>.
    After you downloaded the file, <a href="http://www.new.com">pr oceed
    to the redirected site</a>.

    --
    Mail to my "From:" address is readable by all at http://www.dodgeit.com/
    == ** ## !! ------------------------------------------------ !! ## ** ==
    TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
    may bypass my spam filter. If it does, I may reply from another address!

    Comment

    Working...