Monday, 17 July 2017

How to create and download a csv file in php | export csv file


Export CSV file using fputcsv(). The fputcsv() function accepts an array data. This array converted CSV format and written to the target .csv.


Click to download from here.


<?php  
   // output headers for the file is downloaded rather than displayed  
   header("Content-type: text/csv");  
   header("Content-Disposition: attachment; filename=file.csv");  
   header("Pragma: no-cache");  
   header("Expires: 0");  
   // data for csv file  
   $employees = array(  
     array("Faruk", "Development", "90000"),  
     array("Siyam", "HR", "125000"),  
     array("Jisan", "Marketting", "65000")  
   );  
   // for output stream  
   $file = fopen('php://output', 'w');  
   // for column headings  
   fputcsv($file, array('Name', 'Department', 'Salary'));  
   foreach ($employees as $employee) {  
     //use fputcsv() generate correct csv lines from your array  
     fputcsv($file, $employee);  
   }  
 ?>  

No comments:

Post a Comment

How to create an visitor counter in PHP | hit counter

Total page view in the php. This count hit in the page. <?php session_start(); if(isset($_SESSION['page_view'])){...