PHP Imports and Exports CSV files,

Source: Internet
Author: User

PHP Imports and Exports CSV files,

This example describes how to import and export CSV files in PHP. 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.

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.