实现思路
首先在html文件中,设置导入按钮,及其及其模态框
<button type="button" data-target="#myModal-daoru" class="btn btn-default glyphicon glyphicon-plus" data-toggle="modal">导入</button>
<div class="modal fade" id="myModal-daoru" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">学生信息</h4>
</div>
<div class="modal-body">
<form class="form-inline" role="form" action="{:U(upload)}" method="post" enctype="multipart/form-data">
<div class="form-group">
<label class="sr-only" for="inputfile">文件输入</label>
<input type="file" id="inputfile" name="file">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">导入</button>
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
</div>
</form>
</div>
</div>
</div>
然后在控制器中,先把文件上传功能实现(
先实例化上传类,设置附件上传大小,上传类型,附件上传根目录,设置附件上传目录。
)
public function upload(){
$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->import($upload->rootPath.$info['file']['savepath'].$info['file']['savename']);
}
}
通过写日志来上传文件,首先先判断编码格式,使用detect_encoding函数来判断编码格式,将编码格式转换为utf-8。其次,使用fopen函数打开文件,将文件中的数据放到一个数组中,然后查询数据库中的学号也放入一个数组中,将文件中的数据与用fgetcsv函数读取出的数据这两个数组用array_combine函数合并。然后与数据库中的no用is_array对比是否存在,然后用file_put_contents函数写入文件,最后fclose关闭文件。
public function write($file){
$encoding=detect_encoding($file);
//如果不是utf-8转换utf-8
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);
$arr = array();
while(($row = fgetcsv($fp,1000,","))!== false){
$row=array_combine($fields, $row);
$name = $row['name'];
$row['py'] = SpGetPinyin($name);
if(in_array($row['no'],$arrNO)){
$file = './Public/uploads/demo.txt';
$current .= $row['no']."已存在\r\n";
}else{
$arrNO[]=$row['no'];
$arr[]=$row;
$file = './Public/uploads/demo.txt';
$current .= $row['no']."导入成功\r\n";
}
if(count($arr)==1000){
$model->addAll($arr);
unset($arr);
}
}
if(count($arr)>0){
$model->addAll($arr);
}
$this->success("导入成功",'index');
}
file_put_contents($file, $current);
//文件下载
$file_name = "demo.txt";
$file_dir = "./Public/uploads/";
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;
}
}