/** * 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 = 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; } /** * Import * */ 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 a file exists if ($flag = = = Flase) { if (!file_exists ($path)) { $msg = ' file does not exist '; $flag = true; } } Detecting file formats if ($flag = = = Flase) { $ext = Preg_replace ("/.*." ( [^.] +)/"," $ ", $path); if ($ext! = ' csv ') { $msg = ' Import only csv format file '; $flag = true; } } Detecting 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 to have: '. $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 (' username ', ' password ', ' email '), 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); ?> |