为什么当我尝试使用header('Content-Disposition:attachment; filename="'.$filename.'"');
将MySql表导出为CSV时它没有正确完成:
这是导出它的代码:
$this->view->table = $model->info('name');
$is_csv = $this->_getParam('csv');
if ($this->_request->isPost() && $is_csv) {
$fichier = 'file.csv';
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="'.$fichier.'"');
$fp = fopen('php://output', 'w+');
$data = $model->fetchAll();
foreach ($data as $fields) {
fputcsv($fp, $fields->toArray());
}
fclose($fp);
}
我在这里用按钮调用它:
<form method="post">
<input type="hidden" name="table" value="<?php echo $this->table ?>" />
<button type="submit" class="btn" name="csv" value="csv">
<?php echo Core_Locale::translate('CSV')?>
</button>
</form>
答案 0 :(得分:0)
将此代码放入您的应用程序,然后应用程序创建视图 我有同样的问题,但我这样解决了..
if(isset($_POST['submit'])) {
$csv = new Csv();
$filename = $csv->generateFileDate();
if($filename !== false) {
$data = file_get_contents($csv->folder .'reports/'. $filename);
header('Content-Type: application/csv; charset=utf-8');
header('Content-Disposition: attachement;filename="'.$filename.'";');
echo $data;
exit;
}
}
?>
<!doctype html>
<html lang="us">
<head> .........
希望对你有所帮助