The example in this article describes how PHP can import cvs data into MySQL based on the Fleaphp framework. Share it with everyone for your reference, the details are as follows:
<?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 '备份原始数据失败'; } } } } ?>
Readers who are interested in more PHP-related content can check out the special topics of this site: "PHP Object-Oriented Programming Introductory Tutorial", "Summary of PHP String Usage ", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.