How to Export Data to CSV using PHP?

CSV (Comma Separated Values) is the most widely used and supported file format for transferring tabular data into plain text.

Adding the ability to export data in downloadable CSV is a very much useful feature and becoming common in every website.

Isn’t it?

You might have seen lots of tutorials for implementing CSV Export feature. But, there is always a confusion due to the fact that implementing it practically can run into lots of errors and those errors cannot be easily identified and resolved by a newbie developer.

Hence, I’ve come up with a step-by-step article that will help you implement downloadable CSV export feature using PHP in an easy and efficient manner.

How to export data to CSV?

I have curated 6 simple steps to export data to CSV. This will help you easily implement the CSV export feature within your PHP or PHP based frameworks and content management systems.

Step 1: Create Header

First of all, we need to create an array of the header for CSV file to be created, as we will need to identify what content it is displaying in a tabular format.

$header_args = array( ‘ID’, ’Name’, ’Email’ );

Step 2: Force the output buffer to output CSV with a specific filename

We will proceed with exporting CSV file for download for that we need to add PHP headers as below:

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=csv_export.csv');

Note: If you will not add the PHP header as above then you will not be able to automatically download the CSV file generated, rather it will only display the content.

Step 3: Create a file using PHP

Now, we will create a file pointer to open a writable file which will connect to the output stream before generation of CSV file.

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

Step 4: Clean Up Output Buffer

We will need to clean up the content of output buffer if any so that it will not get written into CSV file or not getting echoed earlier than the actual content generating an Cannot Modify Header Informationerror.


Step 5: Write Header to CSV File

Then, we will write the array of header arguments $header_args which we have created above to the CSV file.

fputcsv($output, $header_args);

Step 6: Write actual content to CSV File

After writing header to CSV file, It’s time to write actual content to CSV file which will output all the data to the CSV file

$data = array(
'0' => array( '1', 'Test 1', '[email protected]' ),
'1' => array( '2', 'Test 2', '[email protected]' ),
'2' => array( '3', 'Test 3', '[email protected]' ),
foreach($data AS $data_item){
    fputcsv($output, $data_item);

That’s it!

Combined Code

I’ve combined the code into one file so that you can use it directly and play with it to know how the CSV Export feature works. You can also try it changing the static content with the dynamic content coming from the database.

Common Errors For CSV Export Feature

I have listed some of the common issues that are faced by novice developer during the development of CSV Export feature. Using this section, you’ll always be aware of the issues or errors to be faced so that you can take precautionary steps while developing CSV Export Feature using PHP.

Let me know via the comments section at the end of the article, if there is any error you’ve faced and it is not listed below so that your comment can help other novice readers.

1. Complete HTML of the page gets written into CSV file

This error will print the whole HTML of the page from<html> to</html> which is not the actual content you want to write to CSV and tracing this issue is the actually difficult part.

Common Cause

  1. Eitherexit; ordie(); is missing after successfully writing data to CSV file.
  2. The output buffer is not cleaned before writing data to CSV file.


  1. Don’t miss to useexit; ordie(); after you have successfully written your data to CSV file.
  2. Make sure you have usedob_end_clean(); before writing data to CSV file so that any garbled data or HTML will not be added to the CSV file.

2. Error: Cannot Modify Header Information – Headers already sent

This error has many possible causes. Also, this error can be solved with many different ways depending upon the area of cause. Still, I’ve shown you the most common area of cause and solution for this error.

Common Cause

The output buffer is either not started at the start of the page or not cleaned before writing data to CSV file.


You must use ob_start();at the start and ob_end_clean();clean output buffer before writing data to any file. This will help you to write your actual data to your file. Missing this 2 functions can lead to garbled data or HTML of page mixed up with actual data.


I’ve tried to cover all the possible errors and ways to make this article more practical and simplified so that each novice developer can easily understand and can try it practically to improve their skills.

CSV Export feature is the most important feature to learn and add it to your skillset. Being a developer, you must add this feature to your skillset.

According to my research, 8 out of 10 projects have CSV Export feature irrespective of the project type.

Hope this bit of information helps!

Leave a Reply

Your email address will not be published. Required fields are marked *