Ejemplo n.º 1
0
 public function txt2array($table_info, $table_name)
 {
     $return = array();
     $fp = opendir(DATA_DIR);
     $data = array();
     while (false != ($file = readdir($fp))) {
         if ($file != '.' && $file != '..' && strstr($file, $table_name . '_')) {
             $tmp_total = hg_get_total_line(DATA_DIR . $file);
             $data = hg_getFileLines(DATA_DIR . $file, 1, $tmp_total);
             @unlink(DATA_DIR . $file);
             break;
         }
     }
     closedir($fp);
     foreach ($data as $k => $v) {
         $v = str_replace(array('|', ',', '\\'), array(',', ',', ''), $v);
         $tmp_data = explode(',', $v);
         $length = count($tmp_data);
         if ($length != count($table_info)) {
             file_put_contents(CACHE_DIR . 'error.log', var_export($v, 1), FILE_APPEND);
             $fp = opendir(DATA_DIR);
             while (false != ($file = readdir($fp))) {
                 if ($file != '.' && $file != '..' && strstr($file, $table_name . '_')) {
                     @unlink(DATA_DIR . $file);
                 }
             }
             closedir($fp);
             return array('error' => TABLE_FIELDS_IS_WRONG);
         }
         for ($i = 0; $i < $length; $i++) {
             $return[$k][$table_info[$i]] = iconv("gbk", "UTF-8", $tmp_data[$i]);
         }
     }
     //hg_pre($return);exit;
     if ($return) {
         foreach ($return as $k => $v) {
             $ret = $this->obj->create($table_name, $v);
             if (isset($ret['error']) && $ret['error']) {
                 return $ret;
             }
         }
         unset($data, $return);
         //$this->txt2array($table_info,$table_name);
     } else {
         return true;
     }
 }
Ejemplo n.º 2
0
 public function txt2array($table_info, $table_name)
 {
     $return = array();
     $fp = opendir(DATA_DIR);
     $data = array();
     while (false != ($file = readdir($fp))) {
         if ($file != '.' && $file != '..' && strstr($file, $table_name . '_')) {
             $tmp_total = hg_get_total_line(DATA_DIR . $file);
             $data = hg_getFileLines(DATA_DIR . $file, 1, $tmp_total);
             @unlink(DATA_DIR . $file);
             break;
         }
     }
     closedir($fp);
     //hg_pre($return);exit;
     if ($data) {
         $table_info_str = $space = '';
         foreach ($table_info as $k => $v) {
             $table_info_str .= $space . "`" . $v . "`";
             $space = ',';
         }
         $sql = '';
         if ($table_info_str) {
             $sql = "INSERT INTO " . DB_PREFIX . $table_name . "(" . $table_info_str . ") values ";
         }
         $extra = $space = '';
         foreach ($data as $k => $v) {
             $v_error = $v;
             $v = str_replace(array('|', ',', '\\'), array(',', ',', ''), $v);
             $tmp_data = explode(',', $v);
             $tmp_extra = $tmp_space = '';
             $length = count($tmp_data);
             if ($length != count($table_info)) {
                 file_put_contents(CACHE_DIR . 'error.log', trim(iconv("gbk", "UTF-8", trim($v_error)), '"') . '-----------', FILE_APPEND);
                 continue;
             }
             $tmp_extra = "(";
             for ($i = 0; $i < $length; $i++) {
                 $tmp_extra .= $tmp_space . (trim($tmp_data[$i]) ? '"' . trim(iconv("gbk", "UTF-8", trim($tmp_data[$i])), '"') . '"' : '""');
                 $tmp_space = ',';
             }
             $tmp_extra .= ")";
             $extra .= $space . $tmp_extra;
             $space = ',';
             unset($tmp_data);
         }
         $sql .= $extra;
         $this->db->query($sql);
         unset($data);
         //echo memory_get_usage();
         //echo "<br/>";
         $this->txt2array($table_info, $table_name);
     } else {
         return true;
     }
 }