CSV (Comma Separated Values) is the most widely used and supported file format for transferring tabular data into plain text. Show 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. 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 HeaderFirst 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 filenameWe 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 PHPNow, 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 BufferWe 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 ob_end_clean(); Step 5: Write Header to CSV FileThen, 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 FileAfter 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); } exit; That’s it! Combined CodeI’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 FeatureI 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 fileThis error will print the whole HTML of the page from Common Cause
Fix
2. Error: Cannot Modify Header Information – Headers already sentThis 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 CauseThe output buffer is either not started at the start of the page or not cleaned before writing data to CSV file. FixYou must use ConclusionI’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! |