
Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughCreating downloadable CSV files in PHP
Parent Feed:
This blog explains, how to create a CSV file using PHP and how to download the file instead of displaying it.
<?php
// Creates a new csv file and store it in tmp directory
$new_csv = fopen('/tmp/report.csv', 'w');
fputcsv($new_csv, $row);
fclose($new_csv);
// output headers so that the file is downloaded rather than displayed
header("Content-type: text/csv");
header("Content-disposition: attachment; filename = report.csv");
readfile("/tmp/report.csv");
?>This code tells the browser that it is generating a CSV file and that should be offered for download, rather than displayed in the browser.
header("Content-type: text/csv");The MIME type of the content is text/csv, the official MIME type for CSV files.
header("Content-disposition: attachment; filename=report.csv");header to supply a recommended filename and force the browser to display the save dialog.
readfile("/tmp/report.csv");readfile — Outputs a file.
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web