Php operation csv file code instance Summary

Source: Internet
Author: User
This article mainly introduces the summary of php csv file code instances. This article provides six sample code for operating CSV files, including reading

This article mainly introduces the summary of php csv file code instances. This article provides six sample code for operating CSV files, including reading

1. Read csv data and output it to the sales.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 ('all regions', '---', '--', 2004 ),); $ 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 ('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 ('all regions', '---', '--', 2004 ),); $ 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 ('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 ('all regions', '---', '--', 2004),); 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' '; Print' ';} Print" \ N ";} print"
'.Html entities ($ csv_line [$ I]).''.Html entities (iconv ("gb2312", "UTF-8", $ csv_line [$ I]).'
\ 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."
";}

,
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.