public static function outputCsv($fileName, $assocData = array()) { $zipFile = '/tmp/' . $fileName . '.zip'; $zip = new ZipArchive(); if ($zip->open($zipFile, ZipArchive::CREATE) !== true) { throw new Exception("Cannot open zip archive"); } if (is_array($assocData) && !empty($assocData)) { foreach ($assocData as $data) { if (!isset($data["data"])) { continue; } $fp = fopen('php://output', 'w'); if (!$fp) { throw new Exception("Unable to open output buffer"); } ob_start(); $names = []; $types = []; foreach ($data["header"] as $column) { $names[] = GlobalHelper::translate($column); $types[] = GlobalHelper::meaningOf($column); } fputcsv($fp, $names); fputcsv($fp, $types); foreach ($data['data'] as $values) { fputcsv($fp, $values); } $string = ob_get_contents(); $zip->addFromString($data['station']->getName() . ".csv", $string); ob_clean(); fclose($fp); } } $zip->close(); if (file_exists($zipFile)) { 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/zip'); header('Content-Disposition: attachment;filename=' . $fileName . '.zip'); header('Content-Length: ' . filesize($zipFile)); header("Content-Transfer-Encoding: binary"); readfile($zipFile); ob_flush(); unlink($zipFile); } else { throw new Exception("The zip file couldn't handle this much data."); } }