Written by Sean Behan on Sat Nov 25th 2017

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 with..
#csv #php #headers #tips #snippets #attachments #webdev

Just finishing up brewing up some fresh ground comments...