Creating CSV file with PHP

  • marlenetrans
  • Newbie
  • Newbie
  • User avatar
  • Posts: 7

Post 3+ Months Ago

Hi,
I been trying to create a CSV file using PHP and here what I've done so far.

Code: [ Select ]
header("Content-Type: application/csv") ;
print $data;
exit();
  1. header("Content-Type: application/csv") ;
  2. print $data;
  3. exit();


Now this, is only viewing the file on the browser, i want it to pop out a download and i can give it a dynamic name and download it. Other thing, how do i set the encoding in the header function? my csv contain special characters that can only be viewed when the file is saved as UTF-8.

Thanks,
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Here's a paper on HTTP Header Definitions.

To specify a utf-8 character set, you append the character set to your Content-Type header preceeded by a semi-colon.
I'm not sure whether this will actually have any effect considering you're aiming for a download instead of display though.

Code: [ Select ]
header('Content-Type: application/csv; utf-8');


As for triggering a download prompt, check out some of the notes and user comments on the php.net readfile function page.
  • marlenetrans
  • Newbie
  • Newbie
  • User avatar
  • Posts: 7

Post 3+ Months Ago

Joebert, thanks a lot.. worked perfectly mwah!

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 43 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.