Google Answers Logo
View Question
 
Q: Serving files and redirecting using headers. ( No Answer,   1 Comment )
Question  
Subject: Serving files and redirecting using headers.
Category: Computers > Programming
Asked by: gooseman-ga
List Price: $15.00
Posted: 01 Dec 2004 01:57 PST
Expires: 16 Dec 2004 13:58 PST
Question ID: 436503
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 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))); 
   fpassthru($fp); 
}

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

header('Location: 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!

PS - I'm looking for a browser compatable solution to this, and not
merely "do this using 2 pages". Thanks :)
Answer  
There is no answer at this time.

Comments  
Subject: Re: Serving files and redirecting using headers.
From: ranjeet_rain-ga on 03 Dec 2004 08:12 PST
 
Hi gooseman,

This behaviour is very predictable. If you look at the HTTP protocol,
it will become very obvious to you. Let me help you recall how a
simple HTTP transaction takes place.

The client (the web browser) sends a request to the server with the
URL to retrieve. The server is free to send back any response. Just a
200 (ok) or 300 (redirection) or 400 (document not found) or return a
document. But under no circumstamces can it return two responses. So,
there is the catch! What you are trying is certainly not possible that
way.

A workable solution in your case would be, if you redirect first and
then in the redirected page, if you can execute a JavaScript function
to open a new window with the URL that will cause the browser to begin
downloading the file. Most of the sites I have seen, use this
technique. IBM.com for example.

Another solution would be, for you to call a javascript function in
the onclick event of the link that causes this. Let me show you how.

function RedirectAndDownload() {
    top.location.href="http://answer.google.com";
    window.open("http://www13.brinkster.com/ranjeetrain/getres.asp?res=ranjeetee.jpg",
"testwin", "");
return false;
}
</SCRIPT>

<A href="#" onclick="return RedirectAndDownload()">Click here to get
the download the file. You will be redirected to the GOOGLE answers as
well</A>

Please feel free to ask your doubts if any.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy