在php中导出之前压缩csv

时间:2016-08-03 19:18:43

标签: php csv

这是我的代码。它工作正常,现在我想在生成之前压缩csv文件。有人请建议我做什么!

$filename = 'Product_Export_' . date('Y-m-d') . '.csv';
header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
header('Content-Disposition: attachment; filename=' . $filename);
echo "\xEF\xBB\xBF"; // UTF-8 BOM
// clean the output buffer
ob_clean();
echo trim($_SESSION['cProductCSVdata']);
$_SESSION['cProductCSVdata'] = '';
exit;

1 个答案:

答案 0 :(得分:1)

使用ZipArchive将其发送到zip文件是您的解决方案。 以下是.txt文件的示例。

<?php
$zip = new ZipArchive();
$filename = "./test112.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}

$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();
?>