我想上传一个csv文件并将其数据导入到student数据表中,以学号为主键,如果表中存在该学号,则返回“no已存在”并存入名为demo.txt的日志文件中,如果不存在该no,则当前行数据存入数据表并返回“no导入成功”存入日志文件中,点击下载按钮,自动下载demo.txt。我们用thinkphp3.2实现。
php代码:(tp3.2.2)
//导入
public function upload(){
if (IS_GET) {
$this->display();
exit();
}
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize = 0 ;// 设置附件上传大小,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);
if ($encoding != 'utf-8') {
$contens = file_get_contents($file);
$contens = mb_convert_encoding($contens,'utf-8',$encoding);
file_put_contents($file, $contens);
}
// exit();
$fp = fopen($file,'r');
if($fp){
$fileds = array('no','name','sex');
$model = D('student');
$arrNo = $model->getField('no',true);
$arr = array();
while(($row = fgetcsv($fp,1000,",")) !== false) {
$row = array_combine($fileds, $row);
$row['py'] = SpGetPinyin($row['name']);
if (in_array($row['no'], $arrNo)) {
//存在
$file = './Public/Download/demo.txt';
$current .= $row['no'] . '已经存在,';
file_put_contents($file, $current);
}else{
//不存在
$arrNo[] = $row['no'];
$arr[] = $row;
$file = './Public/Download/demo.txt';
$current1 .= $row['no'] . '导入成功,';
file_put_contents($file, $current1);
}
if (count($arr) == 1000) {
$model->addAll($arr);
unset($arr);
}
}//while end
if (count($arr)>0) {
$model->addAll($arr);
}
$this->success('执行成功','index');
} // if end
// dump($row);
}//import end
//下载文件
public function download(){
$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 ();
}
}
检验字符串编码格式
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)) {
return $item;
}
}
return null;
}
注意,要使用上传功能 表单需要设置 enctype="multipart/form-data"。
html代码:(bootstrap)
<td>
<input type="button" value="导入" class="btn btn-defaul" data-toggle="modal" data-target="#lead">
</td>
.....
<td>
<a href="{:U('download')}" class="btn btn-defaul">下载</a>
</td>
<!-- 模态框(lead) -->
<div class="modal fade" id="lead" 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>
<h3 class="modal-title" id="myModalLabel">
导入文件信息
</h3>
</div>
<div class="modal-body">
<form action="{:U('upload')}" enctype="multipart/form-data" method="post" >
<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>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div>
<!-- /.modal -->