fanfavorite Posted February 11, 2011 Share Posted February 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/ Share on other sites More sharing options...
MatthewJ Posted February 11, 2011 Share Posted February 11, 2011 Why not just show the progress bar while the file is created, then display a link to a page that will just force the download? It's an extra click, most are not afraid to click an extra time though Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172740 Share on other sites More sharing options...
fanfavorite Posted February 11, 2011 Author Share Posted February 11, 2011 Well I can, I just wanted to avoid the file actual being stored as I have to put a method of deleting it afterwards. Currently the files are just outputted on the fly. Is storing the file the only way I will be able to do this? Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172795 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2011 Share Posted February 11, 2011 Just exactly how long does it take to produce the output data and if it is more than a second or two, why does it take that long? Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172797 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2011 Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172806 Share on other sites More sharing options...
fanfavorite Posted February 11, 2011 Author Share Posted February 11, 2011 Well this isn't for one particular case, but some take over a minute to produce because the database is so large. Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172809 Share on other sites More sharing options...
fanfavorite Posted February 11, 2011 Author Share Posted February 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172813 Share on other sites More sharing options...
PFMaBiSmAd Posted February 11, 2011 Share Posted February 11, 2011 some take over a minute to produce ^^^ You would probably want to find and fix whatever is actually causing that. the database is so large. Databases of 10's of millions of rows shouldn't take that long to retrieve data from. Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172853 Share on other sites More sharing options...
fanfavorite Posted February 11, 2011 Author Share Posted February 11, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/227366-clear-output-to-change-headers/#findComment-1172855 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.