PHP Headers for Sending CSV File Downloads

       

If you would like to force a file download prompt to the user, instead of just outputting the text to the browser, use the following headers.

<?php 
$filename = "some-filename";
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename={$filename}.csv");
header("Pragma: no-cache");
header("Expires: 0");

The key here is setting the Content-Disposition to attachment. You can also tell the browser not to cache and provide a filename as well.

Tagged w/ #csv #php #headers #tips #snippets #attachments #webdev

Apps I've Built

PhotoLocationChanger
Easily change the location on your photos and videos
Click here for more info about the app
PhotoDateChanger
Easily change the dates and times on your photos and videos
Click here for more info about the app
VocabReminder
English dictionary with notifications so you won't forget what you're studying!
Click here for more info about the app
VocabQuiz
The app that quizzes and scores you on your vocabulary!
Click here for more info about the app