示例#1
0
 public function importAction()
 {
     /*设置上传路径*/
     define('INSTALL', str_replace('\\', '/', dirname(dirname(dirname(dirname(dirname(__FILE__)))))));
     $savePath = INSTALL . '/traindata_appui/imgs/video/Excel/';
     $action = !empty($_GET['action']) ? trim($_GET['action']) : '';
     $file = !empty($_GET['file']) ? trim($_GET['file']) : '';
     if ($action == 'del') {
         $myfile = $savePath . $file;
         if (file_exists($myfile)) {
             if (unlink($myfile)) {
                 echo '<script type="text/javascript">alert("删除成功;返回列表");window.location.href="/res/import";</script>';
                 exit;
             }
             echo '<script type="text/javascript">alert("删除失败;返回列表");window.location.href="/res/import";</script>';
             exit;
         } else {
             echo '<script type="text/javascript">alert("文件不存在;返回列表");window.location.href="/res/import";</script>';
             exit;
         }
     } elseif (!empty($_FILES['file_stu']['name']) or $action == 'import') {
         if ($action == 'import' and !empty($file)) {
             $file_name = $file;
         } elseif (!empty($_FILES['file_stu']['name'])) {
             $tmp_file = $_FILES['file_stu']['tmp_name'];
             $file_types = explode(".", $_FILES['file_stu']['name']);
             $file_type = $file_types[count($file_types) - 1];
             /*判别是不是.xls文件,判别是不是excel文件*/
             if (strtolower($file_type) != "xls") {
                 $this->error('不是Excel文件,重新上传');
             }
             if (!is_dir($savePath)) {
                 mkdir($savePath, 0777, true);
             }
             /*以时间来命名上传的文件*/
             $str = date('Ymdhis');
             $file_name = $str . "." . $file_type;
             /*是否上传成功*/
             if (!copy($tmp_file, $savePath . $file_name)) {
                 $this->error('上传失败');
             }
         }
         //echo $savePath . $file_name;exit;
         $res = $this->read($savePath . $file_name);
         /*对生成的数组进行数据库的写入*/
         $errmsg = '';
         $noimpor = '';
         $okmsg = '';
         $m = new Psys_ResModel();
         foreach ($res as $k => $v) {
             if ($k > 1) {
                 $data = array('vname' => $v['1'], 'cast' => $v['6'], 'direcotr' => $v['10'], 'runtimes' => $v['11'], 'hits' => $v['11'], 'price' => $v['14'], 'sectionnum' => $v['15'], 'vpath' => $v['2'], 'vimg' => $v['3'], 'parter' => $v['21'], 'sortid' => $v['18'], 'area' => $v['9'], 'colid' => $v['7'], 'vyear' => $v['8'], 'iftj' => $v['16'], 'flag' => $v['17'], 'vdesc' => $v['4'], 'vdetail' => $v['5'], 'ctime' => time());
                 echo '<pre>';
                 print_r($data);
                 exit;
                 $svl = $obj->GetOne(array('value' => $v[1]), '*', 'rhi_packvalue');
                 if (empty($svl)) {
                     $result = $m->AddVideo($data);
                     if (!$result) {
                         $str = pring_r($data, true);
                         $errmsg .= date('Y-m-d H:i:s') . $str . "\r\n";
                     }
                 } else {
                     $noimpor .= $v[1] . ':在数据库中已存在' . "\r\n";
                 }
             }
         }
         if ($noimpor) {
             $logpas = ERRLOG_PATH . 'video' . DIRECTORY_SEPARATOR . date('Ymd') . DIRECTORY_SEPARATOR;
             if (!is_dir($logpas)) {
                 mkdir($logpas, 0777, true);
             }
             $log = $logpas . 'video_noimport.log';
             $str = $noimpor . "\r\n";
             error_log($str, 3, $log);
         }
         if ($errmsg) {
             $logpas = ERRLOG_PATH . 'video' . DIRECTORY_SEPARATOR . date('Ymd') . DIRECTORY_SEPARATOR;
             if (!is_dir($logpas)) {
                 mkdir($logpas, 0777, true);
             }
             $log = $logpas . 'video_error.log';
             $str = $errmsg . "\r\n";
             error_log($str, 3, $log);
         }
         echo '<script>alert("导入成功,自动过滤从复数据!");window.location.href="/video/valuelist"</script>';
         exit;
     } else {
         //开始运行
         $files = $this->listDir($savePath);
         $this->smarty->assign('files', $files);
         $this->forward = "import";
     }
 }