Creating CSV downloads in PHP
The comma-separated values (CSV) file format is the most widely supported means of representing tabular data. Including a CSV export feature in a web application allows its data to be processed by other applications, increasing its value to users.
This code outputs a simple CSV file and offers it as a download:
Lines 1 and 2 output HTTP headers declaring that the content is UTF-8-encoded CSV data and that the browser should offer to download it. The
Content-Disposition header not only ensures the file is offered as a download rather than displayed in the browser, but also allows a file name to be suggested —
users.csv in this example — rather than leaving the browser to create a file name based on the URL.
Line 4 creates a file pointer that can be used to write to the PHP output stream, and then lines 5 and 6 use the
fputcsv function to output a row of headers and a row of data.
In this example, the list of users is exported from a MySQL database:
Lines 7 to 11 query the database for the list of users. The
MYSQLI_USE_RESULT flag avoids an extra copy of the data being held in memory, allowing larger CSV files to be exported. Lines 12 to 14 then output each row returned by the query.