我有一个表单,在点击提交按钮时对mySQL数据库运行查询。我想捕获结果结果并强制用户下载文件。我得到了一切,但正在下载的文件包含我的HTML页面的源代码。请注意,下面的代码在我的主索引文件中,因此它上面有其他代码,例如doctype,header,body标签。
PHP Warning: fsockopen(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed ..
PHP Warning: fsockopen(): Failed to enable crypto ..
PHP Warning: fsockopen(): unable to connect to tls://dovecot.server:993 (Unknown error) ..
答案 0 :(得分:1)
如果您不需要TXT文件中的html代码(正文,标题,...),则需要在另一个文件中单独使用此代码。
Html代码:
<html>
<body>
<form action="downloadPage.php"></form>
</body>
</html>
PHP(downloadPage.php):
<?
$fp = fopen('php://output', 'w');
ob_start();
if ($fp && $result) {
fputcsv($fp, $headers); # populate header
while ($row = $result->fetch_array(MYSQLI_NUM)) {
fputcsv($fp, array_values($row)); #populate rows
}
}
$content = ob_get_clean();
$filename ='queryResults' . date('Ymd');
// Output CSV-specific headers
header('Pragma: public');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private', false);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . $filename . '.txt";');
exit($content);
?>