Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7161237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:32:00+00:00 2026-05-28T13:32:00+00:00

My goal here is to have the browser download a csv file using headers

  • 0

My goal here is to have the browser download a csv file using headers to do it. For some reason yet to be determined, the browser seems to be downloading the HTML content of the current page (and not the contents of the array I’ve given it).

Here is the code I’ve been using:

$arr1 = array(array("1","2","3","4"),array("2","1","6","6"));

$tmp_handle = fopen('php://memory', 'r+');
fputcsv($tmp_handle, $arr1);

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");

rewind($tmp_handle);
echo stream_get_contents($tmp_handle);

I’ve followed the instructions of many articles / SO questions I’ve read and I don’t see what’s wrong with this code.

I of course appreciate any help that I can get here!

Here is the complete code (upon request):

<?php
global $wpdb;

// Get total number of active referrers
$referrer_check = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."referrer");
$num_of_referrers = 0;

foreach ( $referrer_check as $check) 
{
$num_of_referrers++;
}

// Get total number of referral transactions
$num_of_referrals = 0;
$num_referral_check = $wpdb->get_results("SELECT * FROM ".$wpdb->prefix."referrer_transactions");
foreach ( $num_referral_check as $check) 
{
$num_of_referrals++;
}

// Check for the top referrer
$top_referrer = $wpdb->get_row("SELECT referrer_id, count(*) as row_count FROM ".$wpdb->prefix."referrer_transactions GROUP BY referrer_id ORDER BY COUNT(*) DESC");
$top_referrer_result = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."referrer WHERE referrer_id = $top_referrer->referrer_id");

// Construct the table

// Create array for second table
$ref_transactions_table_arr = array(
array("Referee Name", "Referee ID", "Referee Sign Up", "Referee Email","Referrer ID","Referrer Name"));

foreach ($num_referral_check as $check) 
{
$ref_transactions_table_arr[] = array(
$wpdb->get_var("SELECT billing_name FROM ".$wpdb->prefix."pmpro_membership_orders WHERE user_id = $check->buyer_id"),
$check->buyer_id,
$wpdb->get_var("SELECT user_registered FROM ".$wpdb->prefix."users WHERE ID = $check->buyer_id"),
$wpdb->get_var("SELECT user_email FROM ".$wpdb->prefix."users WHERE ID = $check->buyer_id"),
$wpdb->get_var("SELECT referrer_id FROM ".$wpdb->prefix."referrer WHERE referrer_id = $check->referrer_id"),
$wpdb->get_var("SELECT referrer_name FROM ".$wpdb->prefix."referrer WHERE referrer_id = $check->referrer_id")
);
}

// Create array for first table
$active_ref_table_arr = array(
array('Referrer Name', 'Referrer ID', '# of Referrals', 'Address','Referrer Email','Lifetime Referrals'));

foreach ( $referrer_check as $check) 
{
$active_ref_table_arr[] = array(
$check->referrer_name, 
$check->referrer_id,
$wpdb->get_var("SELECT count(*) FROM ".$wpdb->prefix."referrer_transactions WHERE referrer_id = $check->referrer_id"),
$check->referrer_street . " " . $check->referrer_city . ", " . $check->referrer_state . " " . $check->referrer_zip,
$wpdb->get_var("SELECT user_email FROM ".$wpdb->prefix."users WHERE ID= $check->referrer_id"),
$wpdb->get_var("SELECT count(*) FROM ".$wpdb->prefix."referrer_transactions WHERE referrer_id = $check->referrer_id")
);
}

// Download file
if(isset($_POST['export_tbl_one']))
{
$csvData = array(
  array("1","2","3","4"),
  array("2","1","6","6")
);

$fp = fopen('php://memory', 'w+');

/*foreach ($csvData as $row) {
  fputcsv($fp, $row);
}*/

fputcsv($fp,$csvData);

rewind($fp);
$csvFile = stream_get_contents($fp);
fclose($fp);

header('Content-Type: text/csv');
header('Content-Length: '.strlen($csvFile));
header('Content-Disposition: attachment; filename="file.csv"');

exit($csvFile);
}

?>
<div class="nav">
        <ul>
                <li class="first"><a href="#">Total Referrers: <? echo $num_of_referrers;  ?></a></li>
                <li><a href="#">Total Referals: <? echo $num_of_referrals; ?></a></li>
                <li><a href="#">Top Referrer: <? echo $top_referrer->referrer_id . ", " . $top_referrer_result->referrer_name . "(" . $top_referrer->row_count . ")"; ?></a></li>
<li>
<form method="POST" action="http://keepmecertified.com/acp">
<input type="submit" value="click me" name="export_tbl_one"/>
</form>

</li>
        </ul>
</div>

<br>

<table class="table">
<caption>Referrer Transactions</caption>
<?
$num = 0;

foreach($ref_transactions_table_arr as $fields)
{

  echo "<tr>";


    foreach($fields as $data)
    {

      if($num == 0)
      {
        echo "<th class=\"ref_head\">$data</th>";
      }
      else
      {
        echo "<td>$data</td>";
      }

    }


   echo "</tr>";

   if($num == 0)
   {
     $num++;
   }


}

?>
</table>

<table class="table">
<caption>Active Referrers</caption>
<?
$num = 0;

foreach($active_ref_table_arr as $fields)
{

  echo "<tr>";


    foreach($fields as $data)
    {

      if($num == 0)
      {
        echo "<th class=\"ref_head\">$data</th>";
      }
      else
      {
        echo "<td>$data</td>";
      }

    }


   echo "</tr>";

   if($num == 0)
   {
     $num++;
   }


}

?>
</table>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:32:00+00:00Added an answer on May 28, 2026 at 1:32 pm

    Try this code:

    $csvData = array(
      array("1","2","3","4"),
      array("2","1","6","6")
    );
    
    $fp = fopen('php://memory', 'w+');
    foreach ($csvData as $row) {
      fputcsv($fp, $row);
    }
    
    rewind($fp);
    $csvFile = stream_get_contents($fp);
    fclose($fp);
    
    header('Content-Type: text/csv');
    header('Content-Length: '.strlen($csvFile));
    header('Content-Disposition: attachment; filename="file.csv"');
    
    exit($csvFile);
    

    I have looped the data to build the CSV as your code would not produce the result you expect. I have also retrieved the file as a string before outputting – this is just a nicety to add a Content-Length header. I have also – and this is the important bit – called exit to output the data, to prevent any more code being executed any prevent and HTML after this code being output.

    If you are using this code and still having a problem, then the code is not being called – you should check any if statements etc that this code is wrapped in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So my goal here is to have a single search field in an application
I have a project here the goal is to merge multiple Access DB's into
Here is my ultimate goal... to take this xml file.. <?xml version=1.0?> <Songs> <Song>
The goal here is have my complete database configuration and schema generation handled by
Couple questions here: My end goal is to password protect the file logged_in.php. Note:
My goal here is to create a very simple template language. At the moment,
My goal here is to make a button. I want the text to sit
My goal here is to open temp.php, explode by ### (line terminator), then by
Here's the goal: to replace all standalone ampersands with &amp; but NOT replace those
For example, my goal is to test the code given here: PHP script that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.