|
|
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 :) |
|
There is no answer at this time. |
|
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. |
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 Home - Answers FAQ - Terms of Service - Privacy Policy |