您的位置:首页 > 其它

怎样上传文件,去除重复数据,下载文件

2018-03-29 11:20 309 查看
创建一个uoload方法
上传文件:先实例化上传类,定义上传文件的大小,类型,上传根目录,上传子目录,然后再判断是否上传成功         public function upload(){
if(IS_GET){
    $this->display();
    exit;
}
$upload = new \Think\Upload();//实例化上传类
$upload->maxSize = 0;//设置附件上传大小
$upload->exts = array('csv');//设置附件上传类型
// dump($upload);
// exit();
$upload->rootPath = './Public/Upload/';//设置附件上传根目录
$upload->savePath = '';//设置附件上传(子)目录
//上传文件
$info = $upload->upload();
if(!$info){//上传错误提示错误信息
$this->error($upload->getError());
}else{//上传成功
$this->show('上传成功!');
}
}
去重数据:从数据库里获取学号,存入数组,获取上传文件的学号,用is_array()函数来判断上传的学号是否存在if($fp){
$fields=array('no','name','sex');
$model=D('Student');

<span style="white-space:pre;"> </span> $arrNO=$model->getField('no',true);
$arr = array();
while(($row = fgetcsv($fp,1000,","))!== false){
$row=array_combine($fields, $row);
// dump($data);
// exit;
$name = $row['name'];
$row['py'] = SpGetPinyin($name);

if(in_array($row['no'],$arrNO)){
$file = './Public/uploaddir/demo.txt';
$current .= $row['no']."已存在\r\n";
}else{
$arrNO[]=$row['no'];
$arr[]=$row;
$file = './Public/uploaddir/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/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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: