tp中去除重复,下载


去除重复:

1.关于去掉重复数据导入数据库的基本思路

      (1)首先我们将student数据库里的学号(no)取出来,放在$arrNo数组里。

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

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

2.现在我们有一个上传文件的一个页面:

       <form action="__URL__/upload" style="text-decoration:center;" enctype="multipart/form-data" method="post">
                     <input type="file" name="photo" ><br>
                     <input type="submit" value="导入" style="margin:0 auto;">
                     <input type="button" value="取消"  data-dismiss="modal">

        </form>

注意:enctype="multipart/form-data"这个是上传文件必写的一个属性。

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

    public function upload(){
         if(IS_GET){
          $this->display();
           exit;
         }
         $upload = new \Think\Upload();//实例化上传类
         $upload->maxSize =0;
         $upload->exts = array('csv');//设置附件上的类型
         $upload->rootPath = './Public/upload/';//设置附件上的根目录
         $upload->savePath = '';//设置附件上传(子)目录
         //上传文件
         $info = $upload->upload();
         if(!$info){//上传错误提示错误信息
            $this->error($upload->getError());
         }else{//上传成功
            // $this->success('上传成功'.
          //  $info['file']['savepath'].$info['file']['savename']);
          //  echo$upload->rootPath.
          //  $info['file']['savepath'].$info['file']['savename'],'utf8';
          $this->import($upload->rootPath.$info['file']['savepath'].$info['file']['savename']);
          //$this->success('上传成功');


         }
      } 

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

        public function import($file){       
        // $file = "./Public/Upload/2018-03-21/5ab19fc89b52f.csv";
        //检测文件编码
        $encoding = detect_encoding($file);
        // exit;
        //如果不是utf8格式,则转化为utf8
        if($encoding !='UTF-8'){
          $contens = file_get_contents($file);//file_get_contents — 将整个文件读入一个字符串
          $contens=mb_convert_encoding($contens, 'utf-8',$encoding);//mb_convert_encoding — 转换字符的编码 
          file_put_contents($file, $contens);//file_put_contents — 将一个字符串写入文件
        }
        $fp = fopen($file, 'r');
        if($fp){
          $fields = array('no','name','sex');
          $model = M('student');


          $arrNo=$model->getField('no',true);
          $arr =array();
          while(($row=fgetcsv($fp,1000,","))!==false){//fgetcsv — 从文件指针中读入一行并解析 CSV 字段 
           $row=array_combine($fields,$row);
            $row['py'] = SpGetPinyin($row['name']);


            if(in_array($row['no'], $arrNo)){


             $file =  './Public/Download/demo.txt';
            $current .= $row['no'] . '已经存在,'.PHP_EOL;
             file_put_contents($file, $current);
            }else{
              $arrNo[]=$row['no'];
              $arr[]=$row;
              $file = './Public/Download/demo.txt';
              $current1 .= $row['no'] . '导入成功,'.PHP_EOL;
              file_put_contents($file, $current1);
            }




            if(count($arr)==1000){
              $model->addAll($arr);
              unset($arr);
            }
        }
            if(count($arr)>0){
                $model->addAll($arr);
            }
            // $this->show('添加成功','utf8');
             $this->success('执行成功');
        }
      }  

      最后我们在控制器写一个下载的方法:

             

public function download($file){
      $file_name = 'demo.txt';
        $file_dir = './Public/Download/';
        //检查文件是否存在    
        if (! file_exists ( $file)) {    
            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 ();    
        } 
    } 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值