user804401
user804401

Reputation: 1994

PHP convert comma delimited string to CSV file

I have a delimited string coming from a service, I want to convert this delimited string with headers to a downlodable CSV file.

    empId,empName,salary,depName,depId
    1,John,20000,IT,2

Could someone help me with a code snippet/reference.

Upvotes: 0

Views: 494

Answers (1)

Raghavendra
Raghavendra

Reputation: 3580

change header first

<?php
    header('Content-Disposition: attachment; filename="downloaded.csv"');
    echo 'empId,empName,salary,depName,depId';
    echo '1,John,20000,IT,2';   
?>

and then echo your output.

Upvotes: 1

Related Questions