Home >Backend Development >PHP Tutorial >How to Force CSV File Downloads in PHP?
Problem:
When users click a link, a CSV file opens in the browser window instead of being downloaded.
Code Snippet:
<a href="files/csv/example/example.csv"> Click here to download an example of the "CSV" file </a>
Web Server Configuration:
Approach 1: .htaccess Solution
To force all CSV files on the server to download:
AddType application/octet-stream csv
Approach 2: PHP Solution
To force a specific CSV file to download:
header('Content-Type: application/csv'); header('Content-Disposition: attachment; filename=example.csv'); header('Pragma: no-cache'); readfile("/path/to/yourfile.csv");
Notes:
The above is the detailed content of How to Force CSV File Downloads in PHP?. For more information, please follow other related articles on the PHP Chinese website!