压缩从php导出的csv文件

压缩从php导出的csv文件,php,mysql,csv,zip,compression,Php,Mysql,Csv,Zip,Compression,所以我刚刚启动了PHP,现在我正在编写一个脚本,将mysql查询导出到csv文件中,然后在下载之前对其进行压缩。这是我的密码 <?php $filename = "CSVexport"; // Connect to DB.. $host = 'localhost'; $user = 'root'; //database username $password = ''; // database password $database = 'script'

所以我刚刚启动了PHP,现在我正在编写一个脚本,将mysql查询导出到csv文件中,然后在下载之前对其进行压缩。这是我的密码

    <?php
$filename = "CSVexport";
// Connect to DB..
$host = 'localhost';
$user = 'root';         //database username
$password = '';         // database password
$database = 'script'; // database name
//Connect to MySQL using PDO.
$pdo = new PDO("mysql:host=$host;dbname=$database", $user, $password);
$pdo->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, FALSE);
//tblclients is the table we are acting on
$resultset = $pdo->query('
    SELECT * FROM tblclients  
    ORDER BY id ASC
');
// Flushing less often requires more server memory usage to maintain the buffer
// but is probably faster and does better gzip compression
$flushEveryXRows = 5000;
// Enable GZIP on-the-fly compression
ini_set('zlib.output_compression_level', 9);
ob_start('ob_gzhandler');
//creating my zip file
$zipfile = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($zipfile, ZipArchive::OVERWRITE);
$outputBuffer = fopen("php://temp/maxmemory:$php_memory", 'w');
if($outputBuffer === false) {
    die('Failed to create file');
}
// Create header row of CSV
fputcsv($outputBuffer, [
    'ID', 'uuid', 'FirstName', 'LastName', 'Company Name', 'Email', 'Address', 'address2', 'City', 'State', 'postcode', 'Country', 'Phone Number', 'taxid', 'password', 'authmodule', 'authdata', 'currency', 'defaultgateway', 'credit', 'taxexempt', 'latefeeoveride', 'overideduenotices', 'separateinvoices', 'disableautocc', 'Date created', 'notes', 'billingcid', 'securityqid', 'securityqans', 'groupid', 'cardtype', 'cardlastfour', 'cardnum', 'Start Date', 'Expiry Date', 'issuenumber', 'bankname', 'banktype', 'bankcode', 'bankacct', 'gatewayid', 'lastlogin', 'ip', 'host', 'Status', 'language', 'pwresetkey', 'emailoptout', 'marketingemailsoptin', 'overrideautoclose', 'allow_sso', 'email_verified', 'created at', 'updated at', 'pwresetexpiry'
]);
// Dump all rows
$count = 56;
while ($r = $resultset->fetch(PDO::FETCH_NUM)) {
    fputcsv($outputBuffer, $r);
 $zip->addFromString($filename . ".csv", stream_get_contents($outputBuffer));
}
fclose($outputBuffer);
$zip->close();
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $filename . '.zip"');
header('Content-Length: ' . filesize($zipfile));
readfile($zipfile);
unlink($zipfile);