if($act == 'user_upload_do'){ global $db; $filename = $_FILES['file']['tmp_name']; if (empty ($filename)) { echo '请选择要导入的CSV文件!'; exit; } $handle = fopen($filename, 'r'); $result = input_csv($handle); $len_result = count($result); if($len_result==0){ echo '没有任何数据!'; exit; } for ($i = 1; $i < $len_result; $i++) { //循环获取各字段值 $utype = 1; $company_pwd = 1; $username = "+86".$result[$i][3] ; //手机号 $email =$result[$i][4] ; //邮箱 $mobile = $result[$i][3] ; //手机号 $data_values .= "('$utype','$username','$email','$mobile'),"; } $data_values = substr($data_values,0,-1); //去掉最后一个逗号 fclose($handle); //关闭指针 //var_dump($data_values); //die(); $state = $db->query("insert into sx_members (utype,username,email,mobile) VALUES $data_values "); if($state){ echo '导入成功!'; }else{ echo '导入失败!'; } }
function input_csv($handle) { $out = array (); $n = 0; while ($data = fgetcsv($handle, 10000)) { $num = count($data); for ($i = 0; $i < $num; $i++) { $out[$n][$i] = $data[$i]; } $n++; } return $out; }
用php 本身自带的fgetcsv函数 完全可以实现 没有必要在用phpexel 这些插件