PHP to CSV file read, write, output download _php tutorial

Source: Internet
Author: User

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, ...

  • 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.