在我们导入信息时,会有导入情况来看哪些信息导入了哪些没有导入进去,但是我们不能在页面上显示,如果在页面上显示就会时使用者有不太好的使用体验,所以这是我们就可以写一个下载文件来使用户自主选择是否需要下载,当然下面这个文件的代码我只是实现了导入后直接下载,而且会连同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;
}