本文实例讲述了php基于Fleaphp框架实现cvs数据导入MySQL的方法。分享给大家供大家参考,具体如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
class Controller_KaoqinUpload extends FLEA_Controller_Action {
var $uploaddir = "./uploadfiles/" ;
public function _construct(){
parent::__construct();
}
// $patch="http://localhost/uploadfiles";/
function actionIndex() {
$smarty =& $this ->_getView();
$smarty ->display( "kaoqin_upload.html" );
}
//显示错误
private function showErro(){
error_reporting (E_ALL);
ini_set ( "display_errors" , "On" );
}
//判断日期
private function isDate( $file_name )
{
$filename = explode ( '.' , $file_name );
$real_name = $filename [ count ( $filename )-2]; //得到文件名
$format = "Y-m-d" ; //时间格式类型
$unixTime = strtotime ( $real_name );
$checkDate = date ( $format , $unixTime );
if ( $real_name == $checkDate )
return ture;
else
return false;
}
public function actionSave(){
// $this->showErro();
$upload_file = $_FILES [ 'upload_file' ];
$file_name = $_FILES [ 'upload_file' ][ 'name' ];
$file_tmp_name = $_FILES [ 'upload_file' ][ 'tmp_name' ];
$file_type = $_FILES [ 'upload_file' ][ 'type' ];
$file_size = $_FILES [ 'upload_file' ][ 'size' ];
$file_error = $_FILES [ 'upload_file' ][ 'error' ];
//检查文件
if ( $file_name ==null)
{
echo "文件选择出错,请检查上传文件。" ;
exit ;
}
//判断文件大小
if ( $file_size >=10241024 )
{
$file_size = round ( $file_size / 1048576 * 100) / 100 . ' mb' ;
print_r( "上传的文件大小为" . "$file_size" );
echo "系统只允许上传大小为10M以内的文件。" ;
exit ;
}
//$extention_name = end(explode('.',$_FILES["upload_file"]['name']));//获取扩展名
$extention_name =preg_replace( '/.*/.(.*[^/.].*)*/iU' , '//1' , $file_name );//获得文件的扩展名
//检查文件类型
if ( $file_type != "application/vnd.ms-excel" && $extention_name != "csv" )
{
echo "您上传的文件类型: ." , $extention_name , "<br>" ;
print_r( "系统允许文件类型: .csv" );
exit ;
}
if ( file_exists ( $this ->uploaddir. $_FILES [ 'upload_file' ][ 'name' ]))
{
print ( "备份目录同名数据存在" ); //文件存在
exit ;
}
if ( $this ->isDate( $file_name )==FALSE)
{
print ( "文件命名格式不对,正确格式。例:2010-10-28.csv" ); //文件存在
exit ;
}
// var_dump(file_exists($this->uploaddir.$_FILES['upload_file']['name'])); //test返回
print_r( "原始考勤数据文件:" . $_FILES [ 'upload_file' ][ 'name' ]. "<br>" . "<br>" );
//数据导入
// $fp = fopen($_FILES['upload_file']['tmp_name'], "r");
// $data = fgets($fp, 1000);
// $date=setOutputEncoding('utf-8');
$data =file( $_FILES [ 'upload_file' ][ 'tmp_name' ]);
$attendance =& get_singleton( 'Model_attendance' );
//print_r($data);
//数据导入处理
for ( $i =1; $i < count ( $data )-1; $i ++) {
$a = explode ( ";" , $data [ $i ]);
//编码格式转换
$a [0]=iconv( "gb2312" , "utf-8" , $a [0]);
$a [1]=iconv( "gb2312" , "utf-8" , $a [1]);
$a [2]=iconv( "gb2312" , "utf-8" , $a [2]);
$a [4]=iconv( "gb2312" , "utf-8" , $a [4]);
$a [5]=iconv( "gb2312" , "utf-8" , $a [5]);
// print_r($a);
$t = array ();
$t [ "ATTENDANCE_FINGERPRINT_ID" ]= intval (trim( $a [0], "/" "));
$t [ "ATTENDANCE_USER_NAME" ]=trim( $a [1], "/" ");
$t [ "ATTENDANCE_DATE" ]=trim( $a [2], "/" ");
// $t["ATTENDANCE_DIVISIONS"]=trim($a[3],"/"");//表中字段ATTENDANCE_DIVISIONS在表中删除
$t [ "ATTENDANCE_GO_WORK" ]=trim( $a [4], "/" ");
$t [ "ATTENDANCE_AFTER_WORK" ]=trim( $a [5], "/" ");
//判断打卡情况
$go_work = trim( $a [4], "/" "); //上班时间
$after_work = trim( $a [5], "/" "); //下班时间
// print_r(var_dump($go_work));
// exit();
if ( strlen ( $go_work )==0 && strlen ( $after_work )!=0)
{
$t [ "ATTENDANCE_STATUS" ]= "1" ; //"1"代表正常出勤
}
if ( strlen ( $go_work )!=0 && strlen ( $after_work )==0)
{
$t [ "ATTENDANCE_STATUS" ] = "2" ; //"2"代表上班为打卡
}
if ( strlen ( $go_work )!=0 && strlen ( $after_work )!=0)
{
$t [ "ATTENDANCE_STATUS" ] = "3" ; //"3"代表下班未打
}
if ( strlen ( $go_work )==0 && strlen ( $after_work )==0)
{
$t [ "ATTENDANCE_STATUS" ]= "4" ; //"4"代表未出勤
}
//按考勤规则重置正常上下班时间
if ( strlen ( $go_work ) == 0 && strlen ( $after_work ) == 0)
{
$go_work = "24:00" ;
$after_work = "00:00" ; //未出勤按子时计算
}
if ( strlen ( $go_work ) == 0 && strlen ( $after_work )!=0)
{
$t [ "ATTENDANCE_GO_WORK" ] = $go_work = "08:35" ; //上班未打卡按08:35开始计算
}
if ( strlen ( $go_work ) != 0 && strlen ( $after_work ) == 0)
{
$t [ "ATTENDANCE_AFTER_WORK" ] = $after_work = "17:30" ; //下班为打卡按17:30计算
}
//计算在勤时间
$minutes ; //保存分钟段
$hours ; //保存小时段
$real_time1 = explode ( ":" , $go_work ); //上班时间分割数组
$real_time2 = explode ( ":" , $after_work ); //下班时间分割数组
//开始处理在勤时间
$minutes = intval ( intval ( $real_time2 [1])- intval ( $real_time1 [1]));
if ( $minutes <0)
{
$hours = intval ( intval (( $real_time2 [0])-1)- intval ( $real_time1 [0]));
if ( $hours <=0)
{
$hours = intval (( intval ( $real_time2 [0])-1)- intval ( $real_time1 [0])+24);
}
$minutes = intval ( intval ( $real_time2 [1])+60- intval ( $real_time1 [1]));
$attendance_time = sprintf( "%02d" , $hours ). ":" .sprintf( "%02d" , $minutes );
}
else
{
$hours = intval ( intval ( $real_time2 [0])- intval ( $real_time1 [0]));
if ( $hours <=0)
{
$hours = intval ( intval ( $real_time2 [0])- intval ( $real_time1 [0])+24);
}
if ( $minutes >=10 && $minutes <60) //开始选用strlen判断字符长度补齐位,现在直接用格式化输出,原结构不变。
{ $attendance_time = sprintf( "%02d" , $hours ). ":" .sprintf( "%02d" , $minutes );}
else
{
$attendance_time = sprintf( "%02d" , $hours ). ":" .sprintf( "%02d" , $minutes );
}
}
// 更正未出勤时间情况,去除午休时间的在勤时间
if ( $attendance_time == "-1:00" )
{
$attendance_time = "00:00" ;
}
$t [ "ATTENDANCE_TIME" ]= $attendance_time ; //保存在勤时间
$attendance ->create( $t ); //存入数据库
}
// print_r("数据导入成功")."<br>";
//原始csv文件数据备份,文件保存在系统的./uploadfiles/文件夹下
switch ( $file_error )
{
case 0:
Echo "考勤数据更新成功" . "<br>" ; break ;
case 1:
Echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值." . "<br>" ; break ;
case 2:
Echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。" . "<br>" ; break ;
case 3:
Echo "文件只有部分被上传" . "<br>" ; break ;
case 4:
Echo "没有文件被上传" . "<br>" ; break ;
case 6:
Echo "找不到临时文件夹" . "<br>" ; break ;
case 7:
Echo "文件写入失败" . "<br>" ; break ;
}
echo "<br>" ;
// exit();
// $absolutdir=$_SERVER[DOCUMENT_ROOT ].$uploaddir.$file_name;
if ( $_FILES [ "upload_file" ][ 'error' ]==0)
{ //echo $file_tmp_name;
//echo $this->uploaddir.$_FILES['upload_file']['name'];
//$name=time();
if ( move_uploaded_file( $file_tmp_name , $this ->uploaddir. $_FILES [ 'upload_file' ][ 'name' ]))
{
echo '原始数据备份成功' ;
}
else
{
echo '备份原始数据失败' ;
}
}
}
}
?>
|
希望本文所述对大家PHP程序设计有所帮助。