PHP:zip CSV文件有效但CSV不是内容

时间:2017-06-17 07:10:24

标签: php csv

我有php函数:

function get_partij_download($dbmi, $partij) {
    set_time_limit(0);
    $stmt = $dbmi->prepare("CALL spStartPartijDownload(?)");
    $stmt->bind_param('i', $partij);
    $stmt->execute();
    $result = $stmt->get_result();
    $headers = array('zorgverlener', 'zorgverlener_type', 'jaar','dc_code', 'dc_oms', 'tt_oms', 'zp_code', 'zp_oms', 'zpg_oms', 
    'source1', 'source2', 'source3', 'source4');
    $fp = fopen('php://output', 'w');
    if ($fp && $result) {
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename="export_tarieven.csv"');
        fputcsv($fp, $headers, ";");
        while($row = $result->fetch_assoc()) {
          fputcsv($fp, $row, ";");
        }
    }
    $stmt->close(); 
    return $fp; 
}

这很有用,可以根据需要进行csv下载。 csv也有内容。现在我想首先压缩csv,然后下载zip。我将功能更改为:

function get_partij_download($dbmi, $partij) {      
    set_time_limit(0);
    $stmt = $dbmi->prepare("CALL spStartPartijDownload(?)");
    $stmt->bind_param('i', $partij);
    $stmt->execute();
    $result = $stmt->get_result();
        $headers = array('zorgverlener', 'zorgverlener_type', 'jaar','dc_code', 'dc_oms', 'tt_oms', 'zp_code', 'zp_oms', 'zpg_oms', 
        'source1', 'source2', 'source3', 'source4');
    //$fp = fopen('php://output', 'w');
    $fp = fopen('php://temp/maxmemory:1048576', 'w');
    if ($fp && $result) {
    // create your zip file
        $zipname = 'export_tarvieven_contractpartij.zip';
        $zip = new ZipArchive;
        $zip->open($zipname, ZipArchive::CREATE);
        fputcsv($fp, $headers, ";");
        while($row = $result->fetch_assoc()) {
          fputcsv($fp, $row, ";");
        }
        $zip->addFromString('export_tarieven.csv', stream_get_contents($fp) );
        fclose($fp);
        $zip->close();
    }
    $stmt->close(); 

    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$zipname);
    header('Content-Length: ' . filesize($zipname));
    readfile($zipname);
    unlink($zipname);
}

现在已经下载了zip,但zip(export_tarieven_contractpartij.csv)中的文件没有内容,换句话说:csv为空。有什么想法吗?

此致

0 个答案:

没有答案