I feel like I am missing obvious here, but I will post anyway.
I have a ‘csv.php’ which creates a csv file, simple example:
// csv.php
$fh = fopen('report.csv', 'w') or die("can't open file");
while ($row = mysql_fetch_row($result)) {
fputcsv($fh, $row);
}
fclose($fh);
On a seperate page, there is a simple anchor element that links to the file above. So on home.html there is:
<a href='report.csv'>Report</a>
One user will run csv.php and another will goto home.html and click the link at a later point in time. This works fine, the CSV is created, the data is there, and the anchor links through.
I know that if I were echoing out the CSV, I would add the various headers such as:
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
But how do I add these headers to the file report.csv? I assume this is required because firefox refuses to see the csv as a downloadable file (when the anchor is clicked, it just outputs the csv to the browser).
Headers aren’t part of a file, they’re part of an HTTP response.
If your web server is not setting the headers you want for csv files, you can probably convince it to do so.
Or, if you can’t (or don’t want to), see sberry2A’s answer, to serve the file via php, and control the web server that way.