Download csv from php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Download csv from php

Post by xionhack »

Hello. I have a website done in php. I want to have a hyperlink that when the user clicks on it will download a csv file that would be created in that moment. I've looked in different forums and in the php manual but havent been able to do it. Can somebody help? Thanks!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Download csv from php

Post by requinix »

Code: Select all

<a href="download.php">Download</a>

Code: Select all

<?php // download.php
 
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=filename.csv"); // change the filename
 
// output your CSV content here
 
?>
If you knew to look on the manual page for header you might have seen example #1.
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Re: Download csv from php

Post by xionhack »

Thanks! Now, how should I output the csv. I just know that it has to be separated by |
Post Reply