Creating CSV File

Creating CSV File

CSv stands for Comma Seperated Values

Following example shows how to create a CSV files.

$filename =”file.csv”;

$content = “Test1,Test2″.”\r\n”;
$content .= “Test1,Test2″.”\r\n”;

$fp = fopen ($filename, “w”);
fwrite ($fp,$content,strlen($content));
fclose($fp);

$filesize = filesize($filename);

header(”Content-type: application/octet-stream”);
header(”Content-Disposition: attachment; filename=$filename”);
header(”Pragma: no-cache”);
header(”Expires: 0″);

$fp1 = fopen($filename,”r”);
fpassthru($fp1);
fclose($fp1);

In the above code, first you need to create file. And the datas are seperated by commas.

And following code prompts the users to save or open the file

header(”Content-type: application/octet-stream”);
header(”Content-Disposition: attachment; filename=$filename”);
header(”Pragma: no-cache”);
header(”Expires: 0″);

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.