1.首先实现文件上传,首先form表单中必须有 enctype="multipart/form-data"属性
2.ThinkPHP文件上传操作使用 Think\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();
if(!$info){
$this->error($upload->getError());
}else{
$this->success('上传成功');
}
}
3.判断学号是否重复。首先给学号绑定一个blur事件,离开文本框后弹出该学号是否被添加过。这是一段jquery绑定代码:
$('#no').bind('blur', function(event) {
var no = $("#no").val(); //给no的文本框绑定
$.post('__URL__/validNo',{no:no},function(ret){
// console.log(ret);
if(ret.error){
$('.reg').attr({disabled:false});
alert(ret.msg);
}else{
$('.reg').attr({disabled:true});
alert(ret.msg);
}
},'json')
});
通过ajax传过来,从数据库中读取信息并进行验证
public function validNo(){
if(IS_AJAX){
$no= I('no');
$where['no'] = $no;
$student = M('student')
->field('no,name,sex,tel')
->where($where)
->find();
if($student){
$this->ajaxReturn(array('error'=>false,'student'=>$student,'msg'=>"{$no}已存在"));
}else{
$this->ajaxReturn(array('error'=>true,'student'=>$student,'msg'=>"{$no}没有添加过,可以使用"));
}
}
}
4.通过刚才的验证学号是否重复后,下一步就是将上传的信息添加到数据库中,
$this->success('上传成功');
现在不用这段代码了,而是调用添加的一个方法
$this->import($upload->rootPath.$info['file']['savepath'].$info['file']['savename']);
public function import($file){
// $file="./Public/Upload/2018-03-21/5ab1aee79fcc6.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);
while(($row=fgetcsv($fp,1000,","))!==false){
$row=array_combine($fields, $row);
if(in_array($row['no'], $arrNo)){
$file="./Public/uploaddir/log.txt"; //创建一个日志文件,将导入信息写入这个文件中
$current.=$row['no'].'存在'.',';
// file_put_contents($file, $current); //这里可以用file_put_contents,但是它相当于fopen,fwrite,fclose.所以效率相对较低
fwrite($file, $current); //fwrite更合适一些,之前也用fopen打开过文件,不用重复打开,并将添加信息写入log日志中
}else{
$arrNo[]=$row['no'];
$arr[]=$row;
$file="./Public/uploaddir/log.txt";
$current1.= $row['no'].'导入成功'.',';
// file_put_contents($file, $current1);
fwrite($file, $current1);
}
if(count($arr)==1000){
$model->addAll($arr);
unset($arr);
}
}
$file_name = "log.txt"; //下载文件名
$file_dir = "./Public/uploaddir/"; //下载文件存放目录
//检查文件是否存在
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 ();
}
if(count($arr)>0){
$model->addAll($arr);
}
$this->success('添加成功');
}
}