Export to csv adding html to file

I am trying to export results to a csv file using the following code. The export appears to work, but when I open the file, it contains the html code from the page. Any help is appreciated.

ob_start();
global $wpdb;
$domain = $_SERVER['SERVER_NAME'];
$table = $wpdb-prefix . "qi_project_requests";
$filename = "export.csv";
$sql = $wpdb-get_results("select * from $table");

$header_row = array(
    'Date Submitted',
    'Requestor Name'
);

$data_rows = array();

foreach ($sql as $data) {
    $row = array(
        $data-date,
        $data-rname
    );
    $data_rows[] = $row;
}

$fh = @fopen( 'php://output', 'w' ); 
fprintf( $fh, chr(0xEF) . chr(0xBB) . chr(0xBF) ); 
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); 
header( 'Content-Description: File Transfer' ); 
header( 'Content-type: text/csv' ); 
header( "Content-Disposition: attachment; filename=$filename" ); 
header( 'Expires: 0' ); 
header( 'Pragma: public' ); 
fputcsv( $fh, $header_row ); 
foreach ( $data_rows as $data_row ) { 
    fputcsv( $fh, $data_row ); 
} 
fclose( $fh ); 

ob_end_flush(); 

die(); 

Topic csv export plugins Wordpress

Category Web


I have a similar process to output all email addresses from the database into a downloadable txt file. It looks like this:

$xoutput =  show_subscriber_list();
$xfile = fopen('xsubscriber.txt' , "w") or die("Unable to open file!");;
fwrite($xfile,$xoutput);
fclose($xfile);

$filename = 'xsubscriber.txt'; // of course find the exact filename....        
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false); // required for certain browsers 
header('Content-Type: application/pdf');

header('Content-Disposition: attachment; filename="'. basename($filename) . '";');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filename));

readfile($filename);

exit;

The show_subscriber_list function does the SQL query of the data, getting all the data in a loop through the rows that are returned from the query. That function returns that data as a string, which is used as the content of the $filename (as in return $text_data; ).

The $filename is stored in the current folder. Then that $filename is used in the $header stuff, which gives you the file as a download/open dialog.

The important part is to 'build' the contents of the file before you do the header stuff. You could just as easily 'build' the data into a string, then do the header stuff.

Added

Here is a function that returns the database contents as text. Called with

    $data = mysqli_query($conn, $sql);
    $output = display_data($data); // text output used in the above function

    function display_data($data) {
        $output = "";
        foreach($data as $row) {
            $output .= $row["email"] . PHP_EOL ;
        }


    return $output;
}

And note, as mentioned in comment from OP, that you need to set the proper Content-Type needed for your file contents; I have it set as a PDF.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.