Jump to content

Saving Table Data to CSV File


MikeEller

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.