'Exporting data as two CSV files with PHP, but its stored in one file and also containsHTML tags

I wrote a function in PHP which writes the elements of a two-dimensional array into a CSV file.

function downloadCSV($data)
{
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="sample.csv"');

    $tmp_array = array();
    $anzSpalten = count($data[0]);
    for ($i = 0; $i < $anzSpalten; $i++) {
        array_push($tmp_array, $data[0][$i]);
    }

    $user_CSV[0] = $tmp_array;
    $index = 0;
    foreach ($data as $row) {
        $tmp_array = array();

        for ($i = 0; $i < $anzSpalten; $i++) {
            if (array_key_exists($i, $row)) {
                array_push($tmp_array, $row[$i]);
            } else {
                array_push($tmp_array, "N/A");
            }
        }

        $user_CSV[$index] = $tmp_array;
        $index++;
    }

    $fp = fopen('php://output', 'wb');
    foreach ($user_CSV as $line) {
        // though CSV stands for "comma separated value"
        // in many countries (including France) separator is ";"
        fputcsv($fp, $line, ';');
    }
    fclose($fp);

}

I want that the files to get downloaded by the browser of the user: Picture of procedure

So far, that works, but there are two problems: I call the function twice with two different datasets, but there's just one file downloaded by the browser containing both datasets. I want them to be stored in two separate files.

if( isset($_GET["send"]) & $_GET["send"] == "Download") 
{
    downloadCSV($wetterDat);
    downloadCSV($sensorDat);
}  

Picture of generated and downloaded csv-file

And as you can see in the file above, somehow, the HTML tags are also added to the CSV file, and that's not my intention.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source