This article mainly introduces how to import and export CSV files in PHP. The example analyzes the php operation skills for csv files, which is of great practical value, for more information about how to import and export CSV files in PHP, see the following example. Share it with you for your reference. The details are as follows:
<? Php/*** CSV file processing class */class Csv {public $ csv_array; // csv array data public $ csv_str; // csv file data public function _ construct ($ param_arr, $ column) {$ this-> csv_array = $ param_arr; $ this-> path = $ path; $ this-> column = $ column;}/*** export **/public function export () {if (empty ($ this-> csv_array) | empty ($ this-> column) {return false;} $ param_arr = $ this-> csv_array; unset ($ this-> csv_array); $ export_str = implo De (',', $ param_arr ['Nav']). "n"; unset ($ param_arr ['Nav']); // assemble data foreach ($ param_arr as $ k => $ v) {foreach ($ v as $ k1 => $ v1) {$ export_str. = implode (',', $ v1 ). "n" ;}}// export $ 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 '); o B _start (); // $ file_str = iconv ("UTF-8", 'gbk', $ export_str); ob_end_clean (); echo $ export_str ;} /*** import **/public function import ($ path, $ column = 3) {$ flag = flase; $ code = 0; $ msg = 'unprocessed '; $ filesize = 1; // 1 MB $ maxsize = $ filesize * 1024*1024; $ max_column = 1000; // check whether the file exists if ($ flag = flase) {if (! File_exists ($ path) {$ msg = 'File does not exist'; $ flag = true ;}// check the file format if ($ flag === flase) {$ ext = preg_replace ("/. *. ([^.] +)/"," $1 ", $ path); if ($ ext! = 'Csv') {$ msg = 'only csv format files can be imported '; $ flag = true ;}// check the file size if ($ flag === flase) {if (filesize ($ path)> $ maxsize) {$ msg = 'the imported file cannot exceed '. $ maxsize. 'B file'; $ flag = true ;}// read the 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 = 'The file does not meet the actual specifications :'. $ num. 'column data'; $ flag = true; break;} if ($ f Lag = flase) {for ($ I = 0; $ I <3; $ I ++) {if ($ row = 0) {break ;} // set up data $ dataArray [$ row] [$ I] = $ data [$ I] ;}}$ row ++ ;}return $ dataArray ;}} $ param_arr = array ('Nav' => array ('username', 'password', 'mailbox '), array (0 => array ('xiaohai1', '20170 ', 'xiaohai1 @ zhongsou.com '), 1 => array ('xiaohai2', '000000', 'xiaohai2 @ zhongsou.com '), 2 => array ('xiaohai3', '2017 ', '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 will help you with php programming.