将CSV文件直接下载到用户下载文件夹

Hi I want to have the option on my site for the user to download a CSV file. I have used the code below

 <input type="button" value="Download as CSV file" window.location.href='call_log.csv'  " />

This does work but when the button is clicked the file is opened in another tab on my browser, What I want to happen is a download straight to the users default download folder

I posted this question before and the response was to include headers ie

Content-Disposition: attachment; filename="call_log.csv"

The page is a php file and if I include headers the page does not load but trys to download the whole page . Surely I cant put headers in the CSV file , can anyone help me please ?

Thanks

Your server needs to bet set to execute your php script - you're right; there's no need to change that.

What you need to do is send the correct header to the server from your php script. Here's an example from php.net:

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

For your csv file, the correct content type is text/csv