ThinkPHP文件上传

上传文件,需要在表单中设置 enctype="multipart/form-data" ,下面是一个表单

<form action="{:U('upload')}" enctype="multipart/form-data" method="post" >
	<input type="file" name="file" />
	<input type="submit" value="上传" >
</form>

上传操作,上面的表单提交到upload方法中,下面是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();
        // dump($info);
        if(!$info){
            $this->error($upload->getError());
        }else{
            // $this->success('上传成功!' . $info['file']['savePath'] . $info['file']['savename']);
            $this->import($upload->rootPath . $info['file']['savepath'] . $info['file']['savename']);
        }

    }

上传成功后需要把数据显示到前台,下面写了一个import方法来实现

public function import($file){
        //检测文件编码
        $encoding = detect_encoding($file);
        if($encoding != 'UTF-8'){
            $contens = file_get_contents($file);
            $contens = mb_convert_encoding($contens, 'utf-8',$encoding);
            file_put_contents($file, $contens);
        }

        $fp = fopen($file, 'r');
        if($fp){
            $fields = array('no','name','sex');
            $model = M('student');
            
            $arr = array();
            $newarr=$model->getfield('no',true);
            $open = fopen('./Public/Upload/upload.txt', 'w');
            while (($row=fgetcsv($fp,1000,','))!==false) {
                // dump($row);
                
                $row= array_combine($fields, $row);
                $name = $row['name'];
                $row['py'] = SpGetPinYin($name);
                // dump($arr);
                if(in_array($row['no'], $newarr)){
                    $data .= $row['no'] . "已存在\r\n";
	                fwrite($open,$data);
                }else{
                    $arr[]=$row;
                    $newarr[]=$row['no'];
                }
                
                if(count($arr)==1000){
                    $model->addAll($arr);
                    unset($arr);
                }
            }
            fclose();

            if(count($arr)>0){
                $model->addAll($arr);
            }
            $this->show('添加成功','utf8');
            $this->download();
        }
        
    }
在上面的方法中,先用 detect_encoding检测文件编码格式, 下面是定义的detect_encoding方法
//检测字符串的编码
//$file文件名
 function detect_encoding($file) {
     $list = array('GBK', 'UTF-8', 'UTF-16LE', 'UTF-16BE', 'ISO-8859-1');
     $str = file_get_contents($file);
     foreach ($list as $item) {
         $tmp = mb_convert_encoding($str, $item, $item);
         if (md5($tmp) == md5($str)) {
             return $item;
         }
     }
     return null;
}
再上传数据时还要检测该数据是否已存在,这时候就要去重

打开文件 $fp=fopen($file,'r');  将原有数据表中的所有数据存入一个数组$newarr中,循环读取文件中每一条数据,检测学号在数组$newarr中是否存在,如果不存在,写入$arr中,并且把学号追加到$newarr中;如果存在则写入日志文件中,提示已存在。

下面就是日志下载,方法download

public function download(){
        $file_name = "upload.txt"; //下载文件名    
        $file_dir = "./Public/Upload/"; //下载文件存放目录    
        //检查文件是否存在 
        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 );        
        }
    }






猜你喜欢

转载自blog.csdn.net/tang_tss/article/details/79738290