<?php
/**
 * @var array $headers
 * @var array $rows
 * @var string $filename
 */
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename="' . ($filename ?? 'report.csv') . '"');
header('Pragma: no-cache');
header('Expires: 0');

$output = fopen('php://output', 'w');

fwrite($output, "\xEF\xBB\xBF");

if (!empty($headers)) {
    fputcsv($output, $headers);
}

foreach ($rows as $row) {
    fputcsv($output, (array)$row);
}

fclose($output);
exit;
