Jump to content

Clear Output To Change Headers?


fanfavorite

Recommended Posts

Is there any way to have html output and then clear the output to allow for headers to be changed?  This is still while the php file is executing.  The reason I ask is that I am trying to output a progress bar of an excel file being created.  So while the file is being created by php, I want to output to the browser a progress bar and then once it is complete, I need to set headers so that it downloads it as an excel file.

 

Is this possible or does anyone else have any ideas?  Should I maybe save the file on the web server temporarily and then redirect to the file afterwards?

 

Any help would be appreciated.

 

Thanks,

 

-JC

Link to comment
Share on other sites

I would also ask, how big is the generated data, because you might want to consider saving the resulting 'file' in a session variable so that you can use AJAX to display the progress bar and then once the file has been completely generated, javascript can be used to make a request to your 'download' script that gets the file from the session variable and outputs it.

 

What you are asking takes multiple http requests/http responses, because the only thing you can output for the request/response for the downloaded file are the headers followed by the file data.

Link to comment
Share on other sites

Thanks guys.  I think I will go the route of generating the file and then having a link display after the progress completes. 

 

I've never tried to store that much data in a session variable.  Not even sure it will store properly or what the performance on the server would be.   

Link to comment
Share on other sites

Perhaps you can look at a sample code and let me know how I would optimize it better?  Thanks. 

 

<?
$fn = "regdata.xls";

$header .= '<html xmlns:o="urn:schemas-microsoft-com:office:office"';
$header .= "\n";
$header .= 'xmlns:x="urn:schemas-microsoft-com:office:excel"';
$header .= "\n";
$header .= 'xmlns="http://www.w3.org/TR/REC-html40">';
$header .= "\n";
$header .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
$header .= "\n";
$header .= '<html>';
$header .= "\n";
$header .= '<head>';
$header .= "\n";
$header .= '<meta http-equiv="Content-type" content="text/html;charset=utf-8" />';
$header .= "\n";
$header .= '<style id="Classeur1_16681_Styles">';
$header .= "\n";
$header .= '</style>';
$header .= "\n";
$header .= '</head>';
$header .= "\n";
$header .= "<body>";
$header .= "\n";
$header .= '<div id="Classeur1_16681" align=center x:publishsource="Excel">';
$data .= "\n";
$data .= '<table x:str border=0 cellpadding=0 cellspacing=0 width=100% style="border-collapse: collapse">';
$data .= "\n\t";
$data .= '<tr>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>First Name</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Last Name</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Address</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Address 2</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>City</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Province</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Postal Code</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Phone</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Date of Birth</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Email</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>UPC</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Sign Up</b></td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap><b>Referrals</b></td>';
$data .= "\n\t";
$data .= '</tr>';

$q = mysql_query("SELECT *,max(SignUp) as SignUp,group_concat(UPC) as UPC,group_concat(ID) as ID FROM Entries GROUP BY Email");
while ($f = mysql_fetch_array($q)) {
$data .= "\n\t";
$data .= '<tr>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['FirstName'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['LastName'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['Address'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['Address2'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['City'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['Province'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['PostalCode'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['Phone'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['DOB'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['Email'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['UPC'].'</td>';
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$f['SignUp'].'</td>';
$qa = mysql_query("SELECT group_concat(Distinct Email) as Email FROM ReferAFriend WHERE EntryID IN ($f[iD])");
$fa = mysql_fetch_array($qa);
$data .= "\n\t\t";
$data .= '<td class=xl2216681 nowrap>'.$fa['Email'].'</td>';
$data .= "\n\t";
$data .= '</tr>';
}

$data .= "\n";
$data .= '</table>';
$data .= "\n";
$data .= '</div>';
$data .= "\n";
$data .= '</body>';
$data .= "\n";
$data .= '</html>';

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=".$fn); 
header("Content-Transfer-Encoding: binary ");

print $header.$data;

Link to comment
Share on other sites

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.