这是一个带有附件上传的表单提交:(使用了模态窗口)
<a class="btn btn-default" data-toggle="modal" data-target="#myModal"><span class="glyphicon glyphicon-plus"></span>导入</a>
<!-- 模态框(Modal) -->
<div class="modal fade" id="myModal" 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" method="post" action="__URL__/upload" 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="button" class="btn btn-default" data-dismiss="modal">关闭</button>
<!-- <button type="button" class="btn btn-primary">提交</button> -->
<button type="submit" class="btn btn-default">提交</button>
</div>
</form>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<!-- /.modal -->
注意,要使用上传功能 你的表单需要设置 enctype="multipart/form-data"
导入文件:
//文件上传
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('上传成功!' . $info['file']['savepath'] . $info['file']['savename']);
$file = './Public/Upload/' . $info['file']['savepath'] . $info['file']['savename'];
$this->import($file);
}
}
public function import($file){
// $file="./Public/Upload/2018-03-21/5ab216efebfd2.csv";
//检测文件编码
$encoding=detect_encoding($file);
//如果不是utf8格式,则转化为utf8
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);
// dump($arrno);
// exit;
$arr=array();
$file='download.txt';
$file=fopen($file,'w');
while(($row=fgetcsv($fp,1000,","))!==false){
$row=array_combine($fields, $row);
// dump($arr);
// dump($row);
// exit;
if(in_array($row['no'],$arrno)){
$content=$row['no'] . "已存在" . PHP_EOL;
// file_put_contents($file,$content,FILE_APPEND);//将一个字符串写入文件
}else{
$arrno[]=$row['no'];
$name=$row['name'];
$py=SpGetPinyin($name);
$row['py']=$py;
$password="123456";
$row['password']=md5($password);
$create_time=intval(time());
$row['create_time']=$create_time;
$arr[]=$row;
// $file='download.txt';
$content=$row['no'] . "导入成功" .PHP_EOL;
// file_put_contents($file,$content,FILE_APPEND);
}
fwrite($file, $content);
// dump($row);
if(count($arr)==1000){
$model->addAll($arr);
unset($arr);
}
}
fclose($file);
if(count($arr)>0){
$model->addAll($arr);
}
// $this->success('添加成功');
$this->download();
}
}
设置好上传的参数后,就可以调用Think\Upload
类的upload方法进行附件上传,如果失败,返回false,并且用getError方法获取错误提示信息;如果上传成功,就返回成功上传的文件信息数组。
文件的下载:
//文件下载
public function download(){
$file_name = "download.txt"; //下载文件名
$file_dir = M_DOWN . "/"; //下载文件存放目录
echo $file_dir . $file_name;
//检查文件是否存在
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 ();
}
}