Correct headers to download a CSV from PHP

With these headers you can force a download from php, for example, to let the user download a csv frmatted…

April 27, 2010

With these headers you can force a download from php, for example, to let the user download a csv frmatted text.
To make all the thing works you have to print data with “tabs” char, even if csv means “comma separeted value”. This is necessary because Microsoft Excel will understand tabs, but not commas.

header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"yourfilename.xls\"");
/*
      here print rows with values separeted by tab char: "\t"
*/

Author

I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of Rockit.it and Dailybest.it and I'm actually the Chief Technical Officer of Better Days web agency.

Recommended

Remove archive pages in WordPress, how to

I think that in most blogs (that is not all) archive pages are redundant content and there’s no need to…

September 8, 2015

10 PHP usefull functions for MySQL stuff

Here is my personal collection of 10 php function that I always include in my database function library. I think…

January 25, 2010

Tiny url encode and decode with PHP

The first functions make short urls using tinyurl.com service. It’s a very popular function on the web, you can find…

December 29, 2009

Count coloured cells on Google Sheets with reference

In a spreadsheet if you need to count cells based on their background you can use this code in Google…

May 22, 2020

WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Two post types share the same taxonomy in WordPress

Sharing a WordPress taxonomy between two post types is useful especially if you want to create a relation between different…

November 14, 2019