php导入cvs

导入  cvs  

    // 导入
    public function upload_export()
    {
        if ($_POST['dosubmit']) {

            try {
                $hattach = new hattach();
                $tmpPath = $hattach->uploadfile($_FILES['excel'], array('csv'));
                $myfile = file($tmpPath);
                $i = 0;
                $row_data = array();
                for ($i = 0; $i < count($myfile); $i++) {
                    // 过滤第一条
                    if ($i == 0) {
                        continue;
                    }
                    $encoding = mb_detect_encoding($myfile[$i], array('GB2312', 'GBK', 'UTF-16', 'UCS-2', 'UTF-8', 'BIG5', 'ASCII'));
                    $r = explode(',', $myfile[$i]);
                    $row_data[] = array(
                        'mobile' => iconv($encoding, 'UTF-8', $r[0]),
                        'dateline' => $r[1] ? strtotime(iconv($encoding, 'UTF-8', $r[1])) : time(),
                        'contact' => iconv($encoding, 'UTF-8', $r[2]),
                        'company' => iconv($encoding, 'UTF-8', $r[3]),
                        'organize' => iconv($encoding, 'UTF-8', $r[4]),
                        'job' => iconv($encoding, 'UTF-8', $r[5]),
                        'area' => iconv($encoding, 'UTF-8', $r[6]),
                        'work_year' => iconv($encoding, 'UTF-8', $r[7]),
                        'skill' => iconv($encoding, 'UTF-8', $r[8]),
                        'cy' => iconv($encoding, 'UTF-8', $r[9]),
                        'cy_direction' => iconv($encoding, 'UTF-8', $r[10]),
                        'video' => iconv($encoding, 'UTF-8', $r[11]),
                        'video_name' => iconv($encoding, 'UTF-8', $r[12]),
                        'remark' => iconv($encoding, 'UTF-8', $r[13]),
                        'utm_source' => iconv($encoding, 'UTF-8', $r[14]),
                        'utm_medium' => iconv($encoding, 'UTF-8', $r[15]),
                        'utm_campaign' => iconv($encoding, 'UTF-8', $r[16]),
                        'utm_content' => iconv($encoding, 'UTF-8', $r[17]),
                        'utm_term' => iconv($encoding, 'UTF-8', $r[18]),
                        'source' => iconv($encoding, 'UTF-8', $r[19]),
                        'mobile_status' => iconv($encoding, 'UTF-8', $r[20]),
                        'progress' => $r[21] ? iconv($encoding, 'UTF-8', $r[21]) : 0,
                        'username_remark' => iconv($encoding, 'UTF-8', $r[22]),
                        'ownership' => iconv($encoding, 'UTF-8', $r[23]),
                    );
                }

                foreach ($row_data as $key => $value) {
                    // 缺少手机或者联系人自动过滤
                    if (!$value['mobile'] || intval($value['mobile']) < 100 || !$value['contact']) {
                        continue;
                    }
                    srv_spl_partner_collection::I()->save_collection($value);
                    $ownership=str_replace(array("\n", "\r"), '',  $value['ownership']);
                    if ($ownership){
                        srv_spl_partner_collection::I()->update_collection(array('mobile' => $value['mobile']), array('status' => 2));
                    }
                    
                }
            } catch (Exception $e) {
                hmsg($e->getMessage());
            }
            hmsg('操作成功', '', 1000, 'edit');
        } else {
            include admtpl('admin_partner', 'upload_export');
        }
    }