CSV file upload is not requested

I created my own solution in WordPress that will generate a CSV file that will be uploaded by clicking on just a hyperlink linked directly to that file. Instead of asking you to download the file to your computer; CSV opens in a browser window.

FWIW I'm in Media Temple using a vanilla WordPress installation.

+2


a source to share


4 answers


Send the correct mime type

header('Content-type: text/csv');

      

And use the Content-Disposition header to tell him to download it: http://www.jtricks.com/bits/content_disposition.html



header('Content-Disposition: attachment; filename="mycssfile.csv"');

      

You always want to send the correct mime type, otherwise firewalls, antivirus software and some browsers may have problems with it ...

+4


a source


You can use PHP function header()

to change Content-type

header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="myFile.csv"');

      



The above code will force you to prompt the user to download. where myFile.csv

should be replaced with the path to the file you want to download.

+2


a source


It works:

$filename = 'export.csv';
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);

      

Also, I personally don't like the links on my sites, I like the buttons. If you want the button to do the export function, you can use the code below. I just thought I would post it because it took me a bit to figure it out in the first case :)

<input type="button" value="Export to CSV" onClick="window.location.href='something.php?action=your_action';"/>

      

+2


a source


You need to send the browser the MIME type application/csv

to disable the responsibility for processing the file no matter what the OS recommends (or the user chooses).

In PHP (before sending any result to the client):

header('Content-type: application/csv');

      

0


a source







All Articles