Search code examples
phpwordpresscsvmediatemple

Not being prompted to download CSV file


I've created a custom solution in WordPress that will generate a CSV file to be downloaded by clicking a simple hyperlink, linked directly to this file. Instead of being prompted to download the file to the computer; the CSV opens in the the browser window instead.

FWIW I'm on Media Temple using a vanilla install of WordPress.


Solution

  • Send the proper mime type

    header('Content-type: text/csv');
    

    And use the Content-Disposition header to tell it to download: http://www.jtricks.com/bits/content_disposition.html

    header('Content-Disposition: attachment; filename="mycssfile.csv"');
    

    You always want to send the proper mime type, otherwise firewalls, anti-virus software and some browsers may have issues with it...