php基於Fleaphp架構實現cvs資料匯入MySQL的方法_php技巧

來源:互聯網
上載者:User

本文執行個體講述了php基於Fleaphp架構實現cvs資料匯入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"); } //顯示錯誤 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相關內容感興趣的讀者可查看本站專題:《php物件導向程式設計入門教程》、《php字串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家PHP程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.