MikeEller Posted June 26, 2009 Share Posted June 26, 2009 I have a function to save table data (read from oracle) to a csv file. The function works fine. The problem occurs when I add "header" to the function. If i use header to prompt the user to save the file, the entire page is saved to the file not just what I output to the file. I cannot put the code here...as it is on a different network computer. In a nutshell I have: functionName() { header('Content-type: application/excel'); header('Content-Disposition: attachment; filename="filename.csv"'); readfile('filename.csv'); HTML and php code to display the page Then code to read the data from the database and write it to the file if the user clicks on the "save to excel" button. } Like I said, if I do not use the headers, the file is written perfectly. If I use the headers...the entire page, to include the html markup, buttons, everything, is written to the file. So I am sure I am missusing the headers somehow. Any suggestions? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2009 Share Posted June 26, 2009 if you want to make it so it downloads the file, the headers and the CSV data can be the ONLY thing outputted. If you want to make a page shown while downloading, show the page without the CSV data, then launch a popup, pointing to a page that will output JUST the headers and csv data Quote Link to comment Share on other sites More sharing options...
MikeEller Posted June 26, 2009 Author Share Posted June 26, 2009 OK, That worked. Now when I am prompted to save the file....and I select a place to save the file....it saves it to my selected location but the file is empty. However, the file as I want it to b (with data) is also saved in the folder where the php file resides. Another words, it is not saving data where I want it to. the "filename" in the header is where the file is actually getting saved. Is there a way to override this? Thanks, Mike Quote Link to comment Share on other sites More sharing options...
rhodesa Posted June 26, 2009 Share Posted June 26, 2009 The code should look something like this: <?php if($_GET['download']){ header('Content-type: application/excel'); header('Content-Disposition: attachment; filename="filename.csv"'); //Output CSV here print '"This is some data","Here is some more data","123"'; exit; } ?> <script type="text/javascript"> window.onload = function ( ) { window.open('?download=true'); } </script> Your download should begin automatically if that doesn't help...please post your code Quote Link to comment 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.