生成的文件充满了HTML数据?

时间:2017-08-27 11:02:35

标签: php joomla fopen fwrite

我正在尝试将我的sql数据库中的表写入CSV文件并让用户下载它。但是,下载的文件中包含表单所在站点的html数据。在80行html代码之后显示我自己的数据。我唯一能想到的是某种缓冲区溢出,但我不明白为什么。

这是我的代码:

if (!empty($_POST['inschrijvingen']) && !empty($_POST['jaar'])){
// Create a new query object.
$query2 = $db->getQuery(true);

$query2->select('*');
$query2->from($db->quoteName($TABLE_NAME));
$query2->where($db->quoteName('jaar') .'='.$_POST['jaar']);
$query2->order('naam ASC');

// Reset the query using our newly populated query object.
$db->setQuery($query2);

$data2 = $db->loadAssocList();

$handle = fopen("inschrijvingen_".$_POST['jaar'].".csv", "w");

fwrite($handle, "naam,achternaam,geboortedatum,geslacht,telefoonnummer,email,adres,postcode,opleiding\n");

for ($i = 0; $i < count($data2); $i++){
    fwrite($handle, $data2[$i]['naam'].",".$data2[$i]['achternaam'].",".$data2[$i]['geboortedatum'].",".$data2[$i]['geslacht'].",".$data2[$i]['telefoonnummer'].",".$data2[$i]['email'].",".$data2[$i]['adres'].",".$data2[$i]['postcode'].",".$data2[$i]['opleiding']."\n");
}

fclose($handle);

header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename("inschrijvingen_".$_POST['jaar'].".csv"));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize("inschrijvingen_".$_POST['jaar'].".csv"));
readfile("inschrijvingen_".$_POST['jaar'].".csv");
exit;
}

注意:这是一个joomla模块

0 个答案:

没有答案