PHP csv operation

Source: Internet
Author: User
Tags vars

Write Data for CSV: [PHP]View Plaincopyprint?
  1. $data = Array (
  2. Array (' QQ number ',' login time ', 'name '),
  3. Array (' 123456 ',' 2012-08-21 15:21:10 '. Chr (1),' I'm here to test '),
  4. Array (' 56788 ',' 2012-08-21 18:21:20 '. Chr (1),' test data '),
  5. Array (' 321789 ',' 2012-08-21 11:21:25 '. Chr (1),' HELLO ')
  6. );
  7. $filename = "./file/test.csv";
  8. if (! File_exists ( $filename)) {
  9. File_put_contents ($filename, ");
  10. }
  11. $file = fopen ($filename, ' W ');
  12. foreach ( $data as $val) {
  13. if (false = = = Fputcsv ($file, $val)) {
  14. Die (' Write data failed ');
  15. }
  16. }
  17. Fclose ($file);
$data = Array (' QQ number ', ' Login time ', ' name '), Array (' 123456 ', ' 2012-08-21 15:21:10 '. chr (1), ' I'm here to test '), Array (' 56788 ', ' 2012-08-21 18:21:20 '. chr (1), ' test data '), Array (' 321789 ', ' 2012-08-21 11:21:25 '. chr (1), ' HELLO ')); $filename = "./file/ Test.csv ", if (!file_exists ($filename)) {file_put_contents ($filename, ');} $file = fopen ($filename, ' W '); foreach ($data as $val) {if (false = = = Fputcsv ($file, $val)) {die (' Write Data Failed ')}} Fclose ($file);

There was a problem with my date format when I wrote to CSV, only the format was: 2011/06/05 12:02. Cause my number of seconds does not exist, so at the back of time to add Chr (1) to get the correct format

CSV Read data:

[PHP]View Plaincopyprint?
    1. $file = fopen ($filename, ' W ') or Die (' Open file failed ');
    2. Read data
    3. $file = fopen ($filename, ' R ');
    4. while ( $val = fgetcsv ($file)) {
    5. Print_r ($val);
    6. }
    7. Fclose ($file);
$file = fopen ($filename, ' w ') or Die (' Open file failed ');//Read Data $file =  fopen ($filename, ' R '); while ($val = Fgetcsv ($file)) {pri Nt_r ($val);} Fclose ($file);

Download of CSV:

[PHP]View Plaincopyprint?
  1. $data = Array (
  2. Array (' QQ number ',' login time ', 'name '),
  3. Array (' 123456 ',' 2012-08-21 15:21:10 '. Chr (1),' I'm here to test '),
  4. Array (' 56788 ',' 2012-08-21 18:21:20 '. Chr (1),' test data '),
  5. Array (' 321789 ',' 2012-08-21 11:21:25 '. Chr (1),' HELLO ')
  6. );
  7. Download function
  8. $date = time ();
  9. Header ("Content-type:text/csv");
  10. Header ("content-disposition:attachment; Filename= ". $date. ". csv");
  11. Header (' cache-control:must-revalidate,post-check=0,pre-check=0 ');
  12. Header (' expires:0 ');
  13. Header (' pragma:public ');
  14. foreach ( $data as $val) {
  15. Echo Implode (",", $val)." \ n ";
  16. }
  17. ----------------------------------------------------------------------------
  18. <?php
    $filename = "./creattest.csv";
    Read data
    $file = fopen ($filename, ' R ');
    $prex = "Linpre";
    while ($val = Fgetcsv ($file)) {
    Print_r ($val);
    echo "$val _list[] = $val;
    }
    $val _list[0][0] = $prex. $val _list[0][0];
    $val _list[1][0] = $prex. $val _list[1][0];
    Echo $val _list[0][0]. " ------------";
    $file = fopen ($filename, ' w ');
    foreach ($val _list as $list) {
    if (false = = = Fputcsv ($file, $list)) {
    Die (' Write data failed ');
    }}
    Fclose ($file);

PHP csv operation

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.