This article mainly introduces the summary of php csv file code instances. This article provides six CSV file code instances, including reading, writing, and reading a specified range. For more information, see
1. read csv data and output it to the sales.csv file:
$sales = array( array('Northeast', '2004-01-01', '2004-02-01', 12.54), array('Northwest', '2004-01-01', '2004-02-01', 546.33), array('Southeast', '2004-01-01', '2004-02-01', 93.26), array('Southwest', '2004-01-01', '2004-02-01', 945.21), array('All Regions', '---', '--', 1597.34),);$fh = fopen('sales.csv', 'w') or die("Can't open sales.csv");foreach($sales as $sales_line){ if(fputcsv($fh, $sales_line) === false){ die("Can't write CSV line"); }}fclose($fh) or die("Can't close sales.csv");
2. read csv data and use special stream output
$sales = array( array('Northeast', '2004-01-01', '2004-02-01', 12.54), array('Northwest', '2004-01-01', '2004-02-01', 546.33), array('Southeast', '2004-01-01', '2004-02-01', 93.26), array('Southwest', '2004-01-01', '2004-02-01', 945.21), array('All Regions', '---', '--', 1597.34),);$fh = fopen('php://output', 'w');foreach($sales as $sales_line){ if(fputcsv($fh, $sales_line) === false){ die("Can't write CSV line"); }}fclose($fh);
3. read csv data and output it to the buffer.
$sales = array( array('Northeast', '2004-01-01', '2004-02-01', 12.54), array('Northwest', '2004-01-01', '2004-02-01', 546.33), array('Southeast', '2004-01-01', '2004-02-01', 93.26), array('Southwest', '2004-01-01', '2004-02-01', 945.21), array('All Regions', '---', '--', 1597.34),);ob_start();$fh = fopen('php://output', 'w') or die("Can't open php://output");foreach($sales as $sales_line){ if(fputcsv($fh, $sales_line) === false){ die("Can't write CSV line"); }}fclose($fh) or die("Can't close php://output");$output = ob_get_contents();ob_end_clean();
4. read csv file data
$fp = fopen('sample3.csv', 'r') or die("can't open file");print "
\n";while($csv_line = fgetcsv($fp)){ print '
'; for($i=0, $j=count($csv_line); $i<$j; $i++){ // print '
'.htmlentities($csv_line[$i]).' | '; print '
'.htmlentities(iconv("gb2312","utf-8",$csv_line[$i])).' | '; } print "
\n";}print "
\n";fclose($fp) or die("can't close file");
5. download the CSV file
$ Sales = array ('Northeast ', '2017-01-01', '2017-02-01 ', 2004), array ('northwest', '2017-01-01 ', '2014-02-01 ', 2004), array ('Southeast', '2014-01-01 ', '2014-02-01', 546.33), array ('southwest ', '2017-01-01 ', '2017-02-01', 2004), array ('China', '2017-01-01 ', '2017-02-01', 2004 ),); $ fh = fopen ('php: // output', 'w') or die ("can't open php: // output"); $ total = 0; // tell the browser to send a csv file header ('content-Type: application/csv'); header ('content-Disposition: attachment; filename = "sales.csv "'); // output header fputcsv ($ output, array ('region ', 'Start date', 'end date', 'amount'); // output each row of data, increment $ totalforeach ($ sales as $ sales_line) {if (fputcsv ($ fh, $ sales_line) === false) {die ("Can't write CSV line");} else {$ total + = $ sales_line [3] ;}} fputcsv ($ fh, array ('All regions ', '--', '--', $ total); fclose ($ fh) or die ("Can't close php: // output ");
6. Read the specified row and interval rows of the CSV file.
/***** Read the specified row in the CSV file *****/function get_file_line_a ($ file_name, $ line) {$ n = 0; $ handle = fopen ($ file_name, 'r'); if ($ handle) {while (! Feof ($ handle) {++ $ n; $ out = fgets ($ handle, 4096); if ($ line = $ n) break ;} fclose ($ handle);} if ($ line = $ n) return $ out; return false;} echo get_file_line ("windows_201511s.csv", 10 ); // enter 10th rows/***** read the range row in the CSV file *****/function get_file_line_ B ($ file_name, $ line_star, $ line_end) {$ n = 0; $ handle = fopen ($ file_name, "r"); if ($ handle) {while (! Feof ($ handle) {+ $ n; $ out = fgets ($ handle, 4096); if ($ line_star <= $ n) {$ ling [] = $ out;} if ($ line_end = $ n) break;} fclose ($ handle);} if ($ line_end = $ n) return $ ling; return false;} // use get_file_line to read and output 11th rows to 20th rows $ aa = get_file_line ("windows_201511s.csv", 11, 20 ); // from row 11th to row 20th foreach ($ aa as $ bb) {echo $ bb."
";}