当前位置: 移动技术网 > IT编程>开发语言>PHP > php基于Fleaphp框架实现cvs数据导入MySQL的方法

php基于Fleaphp框架实现cvs数据导入MySQL的方法

2017年12月12日  | 移动技术网IT编程  | 我要评论

席娟作品,船用五金,dvd接显示器

本文实例讲述了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程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网