Create and read CSV file in PHP

Published On: 16 November 2011.By .
  • Digital Engineering

CSV Files stores numbers and text in a plain text foam.Files are easily readable (eg-in a text editor).CSV file are in a simple format which helps consumer,business and scientific applications.

Creating or editing CSV files

CSV file to import and export contact. Program spread like google spreadsheets or Microsoft Excel makes easy to create CSV files.

Steps of creating CSV File.
Create Csv file ::  To create a new csv file we have to create a file with extension of .csv and  set all the data in 2D array .

$list = Array (Array 0=>('2','name','test'),Array1=>('44','name2','test2') )

createcsv(‘path/of/yourfile.csv’,$list);

function createcsv($filepath,$list)
{

$fp = fopen($filepath, ‘w’);

foreach ($list as $fields)
{
fputcsv($fp,$fields);
}

fclose($fp);
return true;
}

Example of  create csv of any html Dynaminc  table data .
pass file  name and the html part

function printHtmlAsCsv($myFile,$HTML)
{

// this code is now for csv file

// convert all html in same format

$HTML=str_replace(“<TR”,”<tr”,$HTML);
$HTML=str_replace(“<TH”,”<td”,$HTML);
$HTML=str_replace(“<TD”,”<td”,$HTML);
$HTML=str_replace(“</TR>”,”</tr>”,$HTML);
$HTML=str_replace(“</TH>”,”</td>”,$HTML);
$HTML=str_replace(“</TD>”,”</td>”,$HTML);
$HTML=str_replace(“</TBODY>”,”</tbody>”,$HTML);
$HTML=str_replace(“</THEAD>”,”</thead>”,$HTML);
$HTML=str_replace(“<TBODY”,”<tbody”,$HTML);
$HTML=str_replace(“<THEAD”,”<thead”,$HTML);

$newhtml = explode(“</tr>”,$HTML);
// total rows in html content
$totrow = count($newhtml);

$totrow= $totrow-2;

// total content in html part
$tottd = explode(‘</td>’,$newhtml[0]);

$tot = count($tottd);
$tot–;

for($tri = 0; $tri<=$totrow; $tri++)
{

$tottd1 = explode(‘</td>’,$newhtml[$tri]);
$str_2 = “”;
for($tdi = 0 ; $tdi<$tot; $tdi++)
{
$td_data[$tdi] =trim($tottd1[$tdi]);
$td_data[$tdi] = str_replace(‘<td>’,”,$td_data[$tdi]);
$td_data[$tdi] = strip_tags($td_data[$tdi]);
// $td_data[$tdi] = str_replace(‘&amp;’,’&’,$td_data[$tdi]);
$td_data[$tdi] =htmlspecialchars_decode($td_data[$tdi]);
$td_data[$tdi] =trim($td_data[$tdi]);
// this is data for create
$str[$tri][] = $td_data[$tdi] ;

}

}
$myFile = $myFile;
createcsv($myFile,$str);

header(‘Content-type: application/csv’);
//open/save dialog box
header(‘Content-Disposition: attachment; filename=”‘.basename($myFile).”.csv”.'”‘);
//read from server and write to buffer
readfile($myFile);

//downloadFile( $myFile );
exit;

}

 
Steps of reading CSV files
CSV data present in a text file format Data table allows to easily review in a variety applications

function readcsvfile($filepath)
{
if (($handle = fopen($filepath, "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>n";
$row++;
for ($c=0; $c < $num; $c++)
{
echo $data[$c] . "<br />n";
}

}

return true;
}
}

 

If anyone require to upload csv file in the project and save data to the data base  And  need to create any csv  file than this code is useful . Creataion of any html table part  in to csv is easy by using this function .

Related content

That’s all for this blog

Go to Top