Php: reading, writing, and downloading csv files _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php provides a detailed description of csv file reading, writing, and output download operations. Copy the code as follows :? Php?filefopen(text.csv, r); while ($ datafgetcsv ($ file) {print_r ($ data) is used to read a line of content in the CSV each time. The code is as follows:


$ File = fopen('text.csv ', 'r ');

While ($ data = fgetcsv ($ file) {// read the contents of a row in the CSV each time.
// Print_r ($ data); // This is an array. to obtain each data, access the array subscript.
$ Goods_list [] = $ data;
}
// Print_r ($ goods_list );
Echo $ goods_list [0] [1];
Fclose ($ file );
?>


In practice, you often need to download some data from the website to a CSV file for later viewing.
Or use CSV to upload data in batches.
In this case, we need to read and write CSV files.

CSV read operations

The code is as follows:


$ File = fopen ('d:/file/file.csv ', 'r ');
While ($ data = fgetcsv ($ file) {// read the contents of a row in the CSV each time.
Print_r ($ data); // This is an array. to obtain each data, access the array subscript.
}
Fclose ($ file );
?>


CSV write operation

The code is as follows:


$ Fp = fopen ('d:/file/file.csv ', 'w ');
Fputcsv ($ fp, array ('AAA', 'BBB ', 'cccccc '));
Fputcsv ($ fp, array ('mmm', 'yyy', 'hahaha'); // fputcsv can be implemented through array loops.
Fclose ($ fp );
?>


Output CSV (download function)

The code is as follows:


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 function)

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";

The http://www.bkjia.com/PHPjc/328114.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/328114.htmlTechArticle code is as follows :? Php $ file = fopen('text.csv ', 'r'); while ($ data = fgetcsv ($ file )) {// read the contents of a line in the CSV each time. // print_r ($ data); // This is an array to obtain...

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.