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>
Advertisement
Answer
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.