Wednesday, 17 July 2013

create a csv file in php

In this post let us check how to create a csv file in php. As a web programmer you may be experienced the need to creating csv based downloadable reports. Like exporting MySQL database to csv.  Here let us check a very simple php code that will help you to create csv report.


Let us check the code to create a csv file in php. In this code I will show how to create a user information list,  after that let us check how the code. So let us start the coding part



$all_users[0]['user_name']    =    "test0";
$all_users[0]['user_email']    =    "email0@email.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$all_users[0]['user_phone']    =    "123-456-7890";
$all_users[0]['user_fax']    =    "5687965878";

$all_users[1]['user_name']    =    "test1";
$all_users[1]['user_email']    =    "email1@email.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$all_users[1]['user_phone']    =    "223-456-7890";
$all_users[1]['user_fax']    =    "3254785412";

$all_users[2]['user_name']    =    "test2";
$all_users[2]['user_email']    =    "email2@email.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$all_users[2]['user_phone']    =    "323-456-7890";
$all_users[2]['user_fax']    =    "5874586587";

$all_users[3]['user_name']    =    "test3";
$all_users[3]['user_email']    =    "email3@email.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$all_users[3]['user_phone']    =    "424-456-7890";
$all_users[3]['user_fax']    =    "3214587564";

$all_users[4]['user_name']    =    "test4";
$all_users[4]['user_email']    =    "email4@email.com<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
</script>";
$all_users[4]['user_phone']    =    "524-456-7890";
$all_users[4]['user_fax']    =    "5741124578";

$users ="name,email,phone,fax\n";

foreach ($all_users as $key=>$value){
$users .= $value['user_name'].",";
$users .= $value['user_email'].",";
$users .= $value['user_phone'].",";
$users .= $value['user_fax']."\n";
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-type: application/csv");
header("Content-Disposition: attachment;filename=users.csv ");
header("Content-Transfer-Encoding: binary ");
echo $users;

In the above code I just used some of the user details. $all_users is a multidimensional array which holds user first name, user email address, user phone and user fax information. Instead of giving this array manually you can use database query to get all information.


$users =”name,email,phone,fax\n”;  line is for setting first row in the csv file . the first row is having name, email, phone and fax.

In the next file foreach ($all_users as $key=>$value){ } is for storing all user information in  $users variable.

In the end of the each line you may noticed that in separated each by comas.

In the last item I used ‘\n’  $users .= $value['user_fax'].”\n”;  this will force next item move to new row.


So from this I hop you will get a basic idea of how to create a csv file in php .




create a csv file in php