牛骨文教育服务平台(让学习变的简单)
博文笔记

浅析tp3的文件上传

创建时间:2018-03-29 投稿人: 浏览次数:158
    在项目中,经常存在大批量像数据中导入信息,但在导入过程中难免存在文件中有重复信息,出现重复导入问题,这就需要我们在后台实现验证。
     我的个人思路:
         1.查询数据库中的学号,将这些学号存在arrNo数组;
         2.创建空数组;
         3.跳入到上传文件的while循环中。判断学号是否在数组中存在,存在则给出提示,该学号已存在;若学号在数组中不存在。追加到存学号的数组中,然后进一步追加到空数组中,然后导入到数据库;
     示例代码:
      public function upload(){
            if(IS_GET){
            $this->display();
            exit;
            }
            $upload = new ThinkUpload();
            $upload->maxSize = 0 ;// 设置附件上传大小
            $upload->exts = array("csv");// 设置附件上传类型
            $upload->rootPath = "./Public/Upload/"; // 设置附件上传根目录
            $upload->savePath = ""; // 设置附件上传(子)目录
            // 上传文件


            $info = $upload->upload();
            
            // dump($info["file"]["savepath"].$info["file"]["savename"]);
            if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
            }else{// 上传成功
            // $this->success("上传成功");
                // dump($upload->rootPath.$info["file"]["savepath"].$info["file"]["savename"]);
            $this->import($upload->rootPath.$info["file"]["savepath"].$info["file"]["savename"]);
            }
    }
    
    public function import($file){
        // $file = "./Public/Upload/2018-03-21/5ab1cb8946cea.csv";
        $encoding = detect_encoding($file);
        if($encoding!="UTF-8"){
            $contents=file_get_contents($file);
            $contents=mb_convert_encoding($contents,"utf-8",$encoding);
            file_put_contents($file, $contents);
        }
        $fp=fopen($file, "r");//打开文件
        if($fp){
            $fields=array("no","name","sex");
            $model=M("student");
            $arrNo=$model->getField("no",true);
            // print_r($arrNo);
            $arr=array();
            $file  = "./Public/Log/log.txt";//
            $fp2 = fopen($file, "w");
            while (($row=fgetcsv($fp,1000,","))!==false){
                $row=array_combine($fields, $row);
                if(in_array($row["no"],$arrNo);){
                    $content = $row["no"]."已存在。" . PHP_EOL; 
                }else{
                    
                    $arrNo[]=$row["no"];
                    $arr[]=$row;
                    $content = "导入成功" . PHP_EOL;                      
                }


                fwrite($fp2, $content);


                if(count($arr)==1000){
                    $model->addAll($arr);
                    unset($arr);
                  }
            }//while end


            fclose($fp2);


            if(count($arr)>0){
                $model->addAll($arr);
            }


            $this->up($file);


    }
}




    文件下载的示例代码:
   public function up($file){ 
            // $file_name = "log.txt";     //下载文件名    
            // $file_dir = "./Public/Log/";        //下载文件存放目录    
            //检查文件是否存在    
            if (! file_exists ( $file )) {    
                echo "文件找不到";    
                exit ();    
            } else {    
                //打开文件    
                $fp = fopen ( $file, "r" );    
                //输入文件标签     
                Header ( "Content-type: application/octet-stream" );    
                Header ( "Accept-Ranges: bytes" );    
                Header ( "Accept-Length: " . filesize ( $file ) );    
                Header ( "Content-Disposition: attachment; filename=" . basename($file) );    
                //输出文件内容     
                //读取文件内容并直接输出到浏览器 
                echo fread ( $fp, filesize ( $file ) );    
                fclose ( $file );    
                exit ();    
            }    
        }
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。