How to force a CSV file to download when clicking on a download link using PHP

Written by Ian Carnaghan · 42 sec read >

Example: We have a CSV file called example.csv we want users to download to use as a template. Now some browsers will display the contents of the file instead of asking users to download the file. What we want to do is to force the downloadinstead of displaying the contents in the browser.

  • Create a new PHP file called ‘download.php
  • Open the file for editing
  • Copy the code below into the ‘download.php‘ file
    {codecitation style=”brush: PHP;”}
    header(‘Content-Type: application/csv’); //Outputting the file as a csv file
    header(‘Content-Disposition: attachment; filename=example.csv’); //Defining the name of the file and suggesting the browser to offer a ‘Save to disk ‘ option
    header(‘Pragma: no-cache’);

    echo readfile(‘example.csv’); //Reading the contents of the file

  • Now on the page where you want the user to click on the link to download the file add the following code
    {codecitation style=”brush: HTML;”}
    <a href=”/download.php”>Click here to download an example of the “CSV” file</a>
  • Now everytime the user clicks on the link the browser won’t display the contents but will offer to download the file

Last Updated On March 22, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

Notify of