PHP implementation of CSV file import and export classes, _php tutorial

Source: Internet
Author: User

PHP implementation of the CSV file import and export classes,


This article describes the import and export classes of the PHP implementation csv file. Share to everyone for your reference. Specific as follows:

<?php/** * CSV file Processing class */class csv{public $csv _array;//csv array data public $csv _str;//csv file data public function __construc    T ($param _arr, $column) {$this->csv_array = $param _arr;    $this->path = $path;  $this->column = $column; }/** * * * */Public Function export () {if (Empty ($this->csv_array) | | empty ($this->column)) {return    False    } $param _arr = $this->csv_array;    unset ($this->csv_array); $export _str = Implode (', ', $param _arr[' nav '). "    n ";    unset ($param _arr[' nav '); Assembly data foreach ($param _arr as $k + $v) {foreach ($v as $k 1=> $v 1) {$export _str. = Implode (', ', $v 1). "      n ";    }}//Export the $EXPORT_STR header ("Cache-control:public");    Header ("Pragma:public");    Header ("Content-type:application/vnd.ms-excel");    Header ("Content-disposition:attachment;filename=txxx.csv");    Header (' Content-type:application/octet-stream ');      Ob_start ();    $file _str= iconv ("Utf-8", ' GBK ', $export _str); Ob_end_cleaN ();  echo $export _str;    * * */Public Function Import ($path, $column = 3) {$flag = flase;    $code = 0;    $msg = ' not processed '; $filesize = 1;    1MB $maxsize = $filesize * 1024 * 1024;     $max _column = 1000;        Detects if the file exists if ($flag = = = Flase) {if (!file_exists ($path)) {$msg = ' file does not exist ';      $flag = true; }}//Detect file format if ($flag = = = flase) {$ext = Preg_replace ("/.*. [^.]      +)/"," $ ", $path);        if ($ext! = ' csv ') {$msg = ' Import only csv format file ';      $flag = true;         }}//Detect file size if ($flag = = = Flase) {if (FileSize ($path) > $maxsize) {$msg = ' The imported file must not exceed '. $maxsize. ' b file ';      $flag = true;      }}//Read file if ($flag = = flase) {$row = 0;      $handle = fopen ($path, ' R ');      $dataArray = Array ();        while ($data = Fgetcsv ($handle, $max _column, ",")) {$num = count ($data);          if ($num < $column) {$msg = ' file does not conform to specifications true: '. $num. ' Column data ';          $flag = true;        Break } if ($flag = = flase) {for ($i =0; $i <3; $i + +) {if ($row = = 0) {break;          }//Build Data $dataArray [$row] [$i] = $data [$i];      }} $row + +;  }} return $dataArray; }} $param _arr = array (' Nav ' =>array (' User name ', ' Password ', ' mailbox '), Array (0=>array (' xiaohai1 ', ' 123456 ', ' Xiaohai1@zhongsou.com '), 1=>array (' Xiaohai2 ', ' 213456 ', ' xiaohai2@zhongsou.com '), 2=>array (' Xiaohai3 ', ' 123456 ', ' xiaohai3@zhongsou.com ')); $column = 3; $csv = new csv ($param _arr, $column);//$csv->export (); $path = ' C: \ Documents and Settings\administrator\temp\txxx.csv '; $import _arr = $csv->import ($path, 3); Var_dump ($import _arr);? >

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/972633.html www.bkjia.com true http://www.bkjia.com/PHPjc/972633.html techarticle PHP Implementation of CSV file import and export classes, this article describes the PHP implementation of CSV file import and export classes. Share to everyone for your reference. The following: php/** * CSV file processing class ...

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