phporcaffeine Posted April 3, 2009 Share Posted April 3, 2009 Here is what I am trying to do: Script creates an .xls file on the fly, forces a download of that file and then redirects the browser window to another page. I have a working example below. The example will create and force a download to the browser but I just can't figure out how to get it to redirect after the download? I know that since I am using the headers to force the download that is what is causing my issue so if someone could show me how to still be able to redirect after the download or maybe a different way to do the download it would be greatly appreciated! <?php function xlsBOF() { echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return; } function xlsEOF() { echo pack("ss", 0x0A, 0x00); return; } function xlsWriteLabel($Row, $Col, $Value ) { $L = strlen($Value); echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); echo $Value; return; } function excelHeader($filename) { header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download");; header("Content-Disposition: attachment;filename=$filename"); header("Content-Transfer-Encoding: binary "); return; } function createOutputFile($extractRows) { excelHeader("contacts.xls"); xlsBOF(); $row=1; foreach ($extractRows as $value) { xlsWriteLabel ($row,0,$value); $row++; } xlsEOF(); } if (isset($_POST['name'])) { $extractRows=array(); $extractRows[]="Some example data 1"; $extractRows[]="Some example data 2"; $extractRows[]="Some example data 3"; $extractRows[]="Some example data 4"; createOutputFile($extractRows); } else { print "<form method=\"post\" id=\"customerdetails\" name=\"calendar_form\" id=\"datesForm\" action=\"$formActionURL\">\n"; print "Extract your contact"; print "<input type=\"text\" id=\"name\" name=\"name\">"; print "<input type=\"submit\" id=\"submitbutton\" value=\"Extract contacts\">"; print "</form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/152414-force-a-download-with-redirect/ Share on other sites More sharing options...
shlumph Posted April 3, 2009 Share Posted April 3, 2009 I'm sorry I can't answer your question, but just out of pure curiosity, why do you place a return statement at the end of your functions? Quote Link to comment https://forums.phpfreaks.com/topic/152414-force-a-download-with-redirect/#findComment-800470 Share on other sites More sharing options...
phporcaffeine Posted April 3, 2009 Author Share Posted April 3, 2009 Your right, it isn't needed but I do it to adhere to coding standards in the project that I am working with. Quote Link to comment https://forums.phpfreaks.com/topic/152414-force-a-download-with-redirect/#findComment-800518 Share on other sites More sharing options...
9three Posted April 3, 2009 Share Posted April 3, 2009 header('Location: http://www.example.com/') if not you can use a refresh though meta <meta http-equiv="refresh" content="3;url=http://www.dalleh.com/"> the number is for the seconds until it redirects the user Quote Link to comment https://forums.phpfreaks.com/topic/152414-force-a-download-with-redirect/#findComment-800594 Share on other sites More sharing options...
MadTechie Posted April 3, 2009 Share Posted April 3, 2009 a simple trick is to have a page called download.php that does the headers and output for the force download.. Then on you page you wish to have the download link and redirect from have the link like so <a href="forcedownload.php?link=myfile.pdf" target="_blank" onclick="javascript:window.location = "mainpage.php">Download</a>; Now that "should" give you a download prompt and then redirect you to mainpage.php Now i know it say target="_blank" which should open a new window but with the file type set correctly the window will not open as you have told it your downloading an attachment.. hope that works helps Quote Link to comment https://forums.phpfreaks.com/topic/152414-force-a-download-with-redirect/#findComment-800613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.