基于ThinkPHP3.2.3框架的导入(防止重复写入)及下载

本文介绍了如何在ThinkPHP3.2.3框架下实现导入数据并防止重复写入的功能,同时提供了一个直接下载导入结果的方案。虽然目前的下载会连同SUCCESS页面一起,但可以通过添加独立的下载按钮来改善用户体验。文章包括html上传代码、php处理上传的代码、下载文档的方法以及格式转换的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在我们导入信息时,会有导入情况来看哪些信息导入了哪些没有导入进去,但是我们不能在页面上显示,如果在页面上显示就会时使用者有不太好的使用体验,所以这是我们就可以写一个下载文件来使用户自主选择是否需要下载,当然下面这个文件的代码我只是实现了导入后直接下载,而且会连同SUCCESS的页面信息一同下载下来,这样会使用户有不太好的体验,你可以自己再写一个按钮调用upload的方法来实现下载,这样就可以使用户自主选择了,这个和上传的方法html写法类似,只需要将action后面的代码和一些没有的东西删掉就可以了,相信你会上传就一定会下载。

接下来让我们来看代码吧。


一、  html代码 上传 我这里是使用的bootstorp写的  和html代码类似 只是不用自己来设计格式了

  

<div class="modal-body">   
         <form action="__URL__/upload" enctype="multipart/form-data" method="post" >//调用php方法upload __URL__当前控制器的地址   注意,要使用上传功能 你的表单需要设置 enctype="multipart/form-data"
                   <input type="file" name="file" />   //上传文件                 
     </div>
              <div class="modal-footer">
                  <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                  <button type="submit" class="btn btn-primary">上传</button>
              </div>
         </form>

二、php代码 upload

public function upload(){
if (IS_GET) {
     $this->display();
     exit;
     }
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 0 ;// 设置附件上传大小
$upload->exts = array('csv');// 设置附件上传类型
$upload->rootPath = './Public/uploads/'; // 设置附件上传根目录
$upload->savePath = ''; // 设置附件上传(子)目录
// 上传文件
$info = $upload->upload();
if(!$info) {// 上传错误提示错误信息
$this->error($upload->getError());
}else{// 上传成功
$this->import($upload->rootPath . $info['file']['savepath'] . $info['file']['savename']);
}
}
public function import($file){
//检测文件编码
$encoding=detect_encoding($file);
//如果不是utf-8格式,转化为utf-8
if($encoding != 'utf-8'){
$contens=file_get_contents($file);//适合小文件(10M以内) file_get_contents()将文件的内容读入到一个字符串中
$contens=mb_convert_encoding($contens, 'utf-8',$encoding);  //
mb_convert_encoding( $str, $encoding1,$encoding2 )   $str,要转换编码的字符串   $encoding1,目标编码,如utf-8,gbk,大小写均可

$encoding2,原编码,如utf-8,gbk,大小写均可


file_put_contents($file, $contens);//保存
}
//解析csv
$fp=fopen($file,'r');//打开文件
if($fp){
$fields=array('no','name','sex');
$model=M('student');
$arrNO=array();
$arrNO=$model->getField('no',true); //获取 no 数组
$arr=array();
$file='./Public/download/demo.txt'; //定义要写入的文档路径
$fop=fopen($file,'w'); //打开文件 写入信息
while(($row=fgetcsv($fp,1000,","))!==false){    //打开csv文件 1000长度,逗号为分界符
// dump($row);
$row=array_combine($fields, $row);  //合并数组
if(in_array($row['no'],$arrNO)){  //检测$row['no'] 在$arrNO是否存在(去重复值)
$current=$row['no']."已存在\r\n";
}else{
$arrNO[]=$row['no'];
$arr[]=$row;
$current=$row['no']."导入成功\r\n";
}
fwrite($fop,$current);  ///写入信息
if(count($arr)==1000){
$model->addAll($arr);   //添加信息
unset($arr);  //情况数组
}
}
fclose($fop);  //关闭文档
if(count($arr)>0){
$model->addAll($arr);   //将剩下的信息导入
}
$this->success('导入操作已完成');
$this->downlaod();  //调用下载导入信息文件
}


}


上面我们有写防止重复添加,我们可以配合这个图来更好的理解


三、下载文档的方法

public function downlaod(){


$file_name = 'demo.txt';  //文件名
        $file_dir = './Public/download/'; //文件路径
        //检查文件是否存在    
        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 ();    
        } 


}



四、转换格式 需要在公共的Common\common 下写function.php文件

 

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)) {   //md5加密模式一样 则返回 $item
             return $item;
         }
     }
     return null;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值