Exporting Processed Data to CSV
CSV export can support download and reuse of a filtered or processed result. It is optional when import already satisfies the data-transfer requirement, but can be valuable for a justified user need.
Send download headers
header("Content-Type: text/csv; charset=UTF-8");
header('Content-Disposition: attachment; filename="team-summary.csv"');
$output = fopen("php://output", "w");
fputcsv($output, ["Team", "Wins", "Losses", "Points"]);
foreach ($rows as $row) {
fputcsv($output, [
$row["team_name"],
$row["wins"],
$row["losses"],
$row["points"]
]);
}
fclose($output);
exit;
Run authentication and authorisation before output headers. Build $rows with the same validated filters used by the visible report.
Test
Open the download in a text editor and spreadsheet. Check headings, quoted commas, character encoding, row count and filtered scope.
Check
- Export serves a real user need.
- Access rules match the data.
-
fputcsv()handles escaping. - Output contains no unintended personal or sensitive data.
- Download matches the on-screen processed result.