· 1 min read

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.

1 comment

  • prajaneil098

    Hello Good morning. I have issue that when i download csv after my url is going to hide.

Leave a comment