Code:
$file = fopen (' text.csv ', ' R ');
while ($data = Fgetcsv ($file)) {//reads a row of CSV each time
Print_r ($data); This is an array, to get every data, access the array subscript can
$goods _list[] = $data;
}
Print_r ($goods _list);
echo $goods _list[0][1];
Fclose ($file);
?>
In the actual work, many times need to download some of the data on the site into a CSV file, easy to view later.
Or use CSV to do some batch upload work.
At this point, we need to read and write the CSV.
Read operations for PHP CSV
Code:
$file = fopen (' d:/file/file.csv ', ' R ');
while ($data = Fgetcsv ($file)) {//reads a row of CSV each time
Print_r ($data); This is an array, to get every data, access the array subscript can
}
Fclose ($file);
?>
Write operations for CSV
Code:
$fp = fopen (' d:/file/file.csv ', ' W ');
Fputcsv ($fp, Array (' AAA ', ' BBB ', ' CCCC ')); Www.jbxue.com
Fputcsv ($fp, Array (' mmm ', ' yyy ', ' haha ')); Fputcsv can be implemented in the form of an array loop
Fclose ($FP);
?>
Fputcsv ($fp, Array (' AAA ', ' BBB ', ' CCCC '));
Fputcsv ($fp, Array (' mmm ', ' yyy ', ' haha ')); Fputcsv can be implemented fclose ($FP) in the form of array loops;
?>
Output csv (download function)
Code:
Header ("Content-type:text/csv");
Header ("content-disposition:attachment; Filename=test.csv ");
Header (' cache-control:must-revalidate,post-check=0,pre-check=0 ');
Header (' expires:0 ');
Header (' Pragma:public ');
echo "id,areacode,areaname/n";
echo "1,cn,china/n";
echo "2,us,america/n";
?>
Output Excel (download feature)
Header ("Content-type:application/vnd.ms-excel");
Header ("Content-disposition:filename=php100.xls");
echo "id,areacode,areaname/n";
echo "1,cn,china/n";
echo "2,us,america/n";
http://www.bkjia.com/PHPjc/670718.html www.bkjia.com true http://www.bkjia.com/PHPjc/670718.html techarticle code: PHP $file = fopen (' text.csv ', ' R '), while ($data = Fgetcsv ($file)) {//read a row of CSV each time//print_r ($data);//This is an array to Get every data, ...