Serving files and redirecting using headers.

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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 sendFileViaHeader($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)(filesize($path . $name)));

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

header('Location: ');

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


Re: Serving files and redirecting using headers.

Gooseman wrote:
Quoted text here. Click to load it

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.

Quoted text here. Click to load it

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_output.php">download the file</a>.
    After you downloaded the file, <a href=" ">proceed
    to the redirected site</a>.

Mail to my "From:" address is readable by all at /
== ** ## !! ------------------------------------------------ !! ## ** ==
may bypass my spam filter. If it does, I may reply from another address!

Site Timeline