php基于Fleaphp框架實(shí)現(xiàn)cvs數(shù)據(jù)導(dǎo)入MySQL的方法
本文實(shí)例講述了php基于Fleaphp框架實(shí)現(xiàn)cvs數(shù)據(jù)導(dǎo)入MySQL的方法。分享給大家供大家參考,具體如下:
<?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"); } //顯示錯(cuò)誤 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";//時(shí)間格式類型 $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 "文件選擇出錯(cuò),請檢查上傳文件。"; exit; } //判斷文件大小 if ($file_size >=10241024 ) { $file_size = round($file_size/ 1048576 * 100) / 100 . ' mb'; print_r("上傳的文件大小為"."$file_size"); echo "系統(tǒng)只允許上傳大小為10M以內(nèi)的文件。"; exit; } //$extention_name = end(explode('.',$_FILES["upload_file"]['name']));//獲取擴(kuò)展名 $extention_name=preg_replace('/.*/.(.*[^/.].*)*/iU','//1',$file_name);//獲得文件的擴(kuò)展名 //檢查文件類型 if($file_type!="application/vnd.ms-excel"&& $extention_name!="csv") { echo "您上傳的文件類型: .",$extention_name,"<br>"; print_r("系統(tǒng)允許文件類型: .csv"); exit; } if(file_exists($this->uploaddir.$_FILES['upload_file']['name'])) { print("備份目錄同名數(shù)據(jù)存在"); //文件存在 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("原始考勤數(shù)據(jù)文件:".$_FILES['upload_file']['name']."<br>"."<br>"); //數(shù)據(jù)導(dǎo)入 // $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); //數(shù)據(jù)導(dǎo)入處理 for($i=1;$i<count($data)-1;$i++) { $a=explode(";",$data[$i]); //編碼格式轉(zhuǎn)換 $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],"/"");//上班時(shí)間 $after_work = trim($a[5],"/"");//下班時(shí)間 // 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"代表未出勤 } //按考勤規(guī)則重置正常上下班時(shí)間 if(strlen($go_work) == 0 && strlen($after_work) == 0) { $go_work = "24:00"; $after_work = "00:00";//未出勤按子時(shí)計(jì)算 } if(strlen($go_work) == 0 && strlen($after_work)!=0) { $t["ATTENDANCE_GO_WORK"] = $go_work = "08:35";//上班未打卡按08:35開始計(jì)算 } if(strlen($go_work) != 0 && strlen($after_work) == 0) { $t["ATTENDANCE_AFTER_WORK"] = $after_work = "17:30"; //下班為打卡按17:30計(jì)算 } //計(jì)算在勤時(shí)間 $minutes;//保存分鐘段 $hours;//保存小時(shí)段 $real_time1 = explode(":",$go_work);//上班時(shí)間分割數(shù)組 $real_time2 = explode(":",$after_work);//下班時(shí)間分割數(shù)組 //開始處理在勤時(shí)間 $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判斷字符長度補(bǔ)齊位,現(xiàn)在直接用格式化輸出,原結(jié)構(gòu)不變。 {$attendance_time = sprintf("%02d", $hours).":".sprintf("%02d", $minutes);} else { $attendance_time = sprintf("%02d", $hours).":".sprintf("%02d", $minutes); } } // 更正未出勤時(shí)間情況,去除午休時(shí)間的在勤時(shí)間 if($attendance_time=="-1:00") { $attendance_time="00:00"; } $t["ATTENDANCE_TIME"]=$attendance_time;//保存在勤時(shí)間 $attendance->create($t);//存入數(shù)據(jù)庫 } // print_r("數(shù)據(jù)導(dǎo)入成功")."<br>"; //原始csv文件數(shù)據(jù)備份,文件保存在系統(tǒng)的./uploadfiles/文件夾下 switch ($file_error) { case 0: Echo "考勤數(shù)據(jù)更新成功"."<br>"; break; case 1: Echo "上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值."."<br>"; break; case 2: Echo "上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。"."<br>"; break; case 3: Echo "文件只有部分被上傳"."<br>";break; case 4: Echo "沒有文件被上傳"."<br>";break; case 6: Echo "找不到臨時(shí)文件夾"."<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 '原始數(shù)據(jù)備份成功'; } else { echo '備份原始數(shù)據(jù)失敗'; } } } } ?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計(jì)有所幫助。
- 將sqlite3中數(shù)據(jù)導(dǎo)入到mysql中的實(shí)戰(zhàn)教程
- MySQL中數(shù)據(jù)導(dǎo)入恢復(fù)的簡單教程
- PHP把MSSQL數(shù)據(jù)導(dǎo)入到MYSQL的方法
- MySQL中使用load data命令實(shí)現(xiàn)數(shù)據(jù)導(dǎo)入的方法
- 解析csv數(shù)據(jù)導(dǎo)入mysql的方法
- phpMyAdmin下將Excel中的數(shù)據(jù)導(dǎo)入MySql的圖文方法
- 3步搞定純真IP數(shù)據(jù)導(dǎo)入到MySQL的方法詳解
- Excel數(shù)據(jù)導(dǎo)入Mysql數(shù)據(jù)庫的實(shí)現(xiàn)代碼
- 如何把ACCESS的數(shù)據(jù)導(dǎo)入到Mysql中
- MYSQL大數(shù)據(jù)導(dǎo)入
- 分析Mysql大量數(shù)據(jù)導(dǎo)入遇到的問題以及解決方案
相關(guān)文章
PHP數(shù)組操作——獲取數(shù)組最后一個(gè)值的方法
這篇文章主要介紹了PHP數(shù)組操作——獲取數(shù)組最后一個(gè)值的方法,需要的朋友可以參考下2015-04-04php生成百度sitemap站點(diǎn)地圖類函數(shù)實(shí)例
這篇文章主要介紹了php生成百度sitemap站點(diǎn)地圖類函數(shù)的方法,詳細(xì)講述了百度站點(diǎn)sitemap的實(shí)現(xiàn)方法與注意事項(xiàng),在web站點(diǎn)的建設(shè)中非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-10-10linux系統(tǒng)上支持php的 iconv()函數(shù)的方法
iconv函數(shù)庫能夠完成各種字符集間的轉(zhuǎn)換,是php編程中不可缺少的基礎(chǔ)函數(shù)庫。2011-10-10詳解PHP+AJAX無刷新分頁實(shí)現(xiàn)方法
這篇文章主要介紹了詳解PHP+AJAX無刷新分頁實(shí)現(xiàn)方法,需要的朋友可以參考下2015-11-11深入file_get_contents與curl函數(shù)的詳解
本篇文章是對file_get_contents與curl函數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)生成數(shù)據(jù)字典功能,涉及php針對mysql常見的連接、數(shù)據(jù)表查詢、遍歷、table表格構(gòu)成等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05php獲取今日開始時(shí)間和結(jié)束時(shí)間的方法
本篇文章實(shí)例講述了php獲取今日開始時(shí)間和結(jié)束時(shí)間的方法,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02