tp文件导入(去除重复数据)写日志,下载

该博客介绍了如何处理CSV文件导入数据库时去除重复数据的方法。首先从数据库中获取现有学号,然后逐行检查CSV文件中的学号是否在数组中,避免插入重复数据。导入过程包括文件上传、编码检查、读取CSV数据,并使用ThinkPHP框架进行操作。同时,博客还展示了如何写入日志记录导入详情以及提供文件下载功能。

    去除重复即插入数据的时候,为了避免插入相同的数据

    1.首先从数据库里的学号取出来,放在数组里。

    2.然后,我们再将csv文件的学号取出来,判断学号是否存在数组。

    3.如果学号在数组中不存在,则将学号追加到,然后将记录存放到$arr数组。如果存在,则不插入。

我们在控制器写一个上传的upload方法:

      public function upload(){
              $upload = new \Think\Upload();// 实例化上传类
              $upload->maxSize = 0 ;// 设置附件上传大小
              $upload->exts = array('csv');// 设置附件上传类型
              $upload->rootPath = './Uploads/'; // 设置附件上传根目录

              $upload->savePath = ''; // 设置附件上传(子)目录

              $info = $upload->upload();//获取上传的信息

              if(!$info) {// 上传错误提示错误信息
           $this->error($upload->getError());
      }else{// 上传成功
           $this->import($upload->rootPath.$info['file']['savepath'].$info['file']['savename']);

               }

             }

     }

在控制器写一个导入数据库的import方法:

       public function import($file){

              $encoding=detect_encoding($file);
              //如果不是utf8格式,则转换为utf8
                   if($encoding != 'UTF-8'){
                       $contens=file_get_contents($file);
                       $contens=mb_convert_encoding($contens, 'utf-8',$encoding);
                       file_put_contents($file, $contens);

                   }

                         $student=M('student');

                         $arrNo=$student->getField('no',true);

                         $fp=fopen($file,'r');//以读的方式打开

                              if($fp){
                              $fields=array('no','name','sex');
                   $model=M('student');
                   $arrNo=$model->getField('no',true);
                    // dump($arrNo);

                   $arr=array();//定义一个空数组

                                   $i=1;//定义行数

                                       while (($row=fgetcsv($fp,1000,","))!==false) {
                       $row=array_combine($fields, $row);
                       // dump($row);
                             if (in_array($row['no'], $arrNo)) {//判断csv的学号在数据库中是否存在
                                  $tr.= '第'.$c.'行'.$row['no']."存在\r\n";
                             }else{
                                   $arrNo[]=$row['no'];
                                   $arr[]=$row;
                                   $tr.='第'.$c.'行'.$row['no']."导入成功\r\n";        
                 }
                                   if (count($arr)==1000) {
                                       $model->addAll($arr);
                                       unset($arr);
                                       $this->show('导入成功','student/index');

                                     }

                                                                 $i++;

               }

                                                                     $data=writelog("myerror",$tr);//调用写日志方法
                                          if (count($arr)>0) {
                                              $model->addAll($arr);
                                              $this->success('导入成功','index');
                                         }else{
                                                                              $this->error('数据库已存在');
                                          }

                            }

                    }

        }

写日志:确定写入文件函数fwriter

function.php中写一个写日志函数:

                 function writelog($type="",$content=""){
                         if(!$content || !$type){
                                 return false;
                        }    
                               $dir=getcwd().DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.$type;
                                      if(!is_dir($dir)){ 
                                           if(!mkdir($dir)){
                                                 return false;
                                           }
                                      }
                                           $filename=$dir.DIRECTORY_SEPARATOR.date("Ymd",time()).'.log';   
                                            $logs=include $filename;
                                                    if($logs && !is_array($logs)){
                                                              unlink($filename);
                                                             return false;
                                                    }
                                                                $logs=$content;
                                                               $str=var_export($logs, true).";";
                                                                       if(!$fp=@fopen($filename,"wb")){
                                                                                 return false;
                                                                         }           
                                                                               if(!fwrite($fp, $str))return false;
                                                                                     fclose($fp);
                                                                                      return true;

                                                                             } 

下载:

1.获取要下载的文件名(加上路径);2.判定文件是否存在;3.执行下载的文件名;4.指定文件的大小;5.输出内容。

public function updown(){
                           $file_dir = "./Logs/myerror/"; 
               $file_name = date("Ymd",time()).'.log'; 
                                             if (!file_exists ($file_dir.$file_name )) {    
    echo "文件找不到";    
    exit ();    
} else {    
    //打开文件    
    $file = fopen ( $file_dir . $file_name, "r" );    
    //输入文件标签     
    Header ( "Content-type: application/octet-stream" );    
    Header ( "Accept-Ranges: bytes" );    
    Header ( "Accept-Length: " . filesize ( $file_dir . $file_name ) );    
    Header ( "Content-Disposition: attachment; filename=" . $file_name );    
    //输出文件内容     
    //读取文件内容并直接输出到浏览器    
    echo fread ( $file, filesize ( $file_dir . $file_name ) );    
    fclose ( $file );    
    exit ();    

               }




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值