Jump to content

Recommended Posts

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>"; 
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/152414-force-a-download-with-redirect/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.