PHP based on fleaphp framework implementation of CVS data import MySQL method _php tips

Source: Internet
Author: User
Tags explode html form sprintf strlen

This example describes the PHP approach to implementing CVS data into MySQL based on the fleaphp framework. Share to everyone for your reference, specific 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");
  ///Display error private Function Showerro () {error_reporting (E_all);
 Ini_set ("Display_errors", "on");
 }//Judgment date Private function isDate ($file _name) {$filename = explode ('. ', $file _name);
  $real _name = $filename [Count ($filename) -2];//gets the filename $format = "y-m-d";//Time format type $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 ']; Check the file if ($file _name==null) {Echo File selection error, please check the upload file.
    ";
   Exit }//Judge file size if ($file _size >=10241024) {$file _size = round ($file _size/1048576 * 100)/100.
    ' MB '; Print_r ("uploaded file size is".)
  $file _size "); echo "System only allows uploading of files of a size of 10M.
  ";
   Exit }//$extention _name = end (Explode ('. ', $_files["upload_file"] [' name ']);//Get extension $extention _name=preg_replace ('/.*/. *[^/.]. *) */iu ', '//1 ', $file _name)//Get File extension//Check file type if ($file _type!= "application/vnd.ms-excel" && $extention _name
  != "CSV") {echo "The type of file you uploaded:.", $extention _name, "<br>";
  Print_r ("System allowed file type:. csv");
   Exit
  } if (file_exists ($this->uploaddir.$_files[' upload_file ' [' name '])) {print ("The backup directory has the same name");//file exists exit; The IF ($this->isdate ($file _name) ==false) {print ("Incorrect file name format, correct format.") Example: 2010-10-28.csv ");
  File exists exit; }//Var_dump (file_exists ($this->uploaddir.$_files[' upload_file '] [' name ']); Test returns Print_r ("Original absence data file:". $_files[' Upload_file '] [' name ']. " <br> "."
  <br> ");
Data import//$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);
   Data import processing for ($i =1 $i <count ($data)-1; $i + +) {$a =explode (";", $data [$i]);
   encoding format conversion $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], "/"///table field attendance_divisions Delete $t ["Attendance_go_work"]=trim ($a [4], "/") in the table;
   $t ["Attendance_after_work"]=trim ($a [5], "/"); 
   The timing $go _work = Trim ($a [4], "/") $after _work = Trim ($a [5], "/");//work Time//Print_r (Var_dump ($go _work));
   Exit (); if (strlen ($go _work) ==0 && strlen ($after _work)!=0) {$t ["Attendance_status"]= "1";//"1" stands for normal attendance} if
    (Strlen ($go _work)!=0 && strlen ($after _work) ==0)
    {$t ["attendance_status"] = "2";//"2" represents clock-in for work} if (Strlen ($go _work)!=0 && strlen ($after _work)!=0)
     {$t ["attendance_status"] = "3";//"3" stands for strlen} if ($go _work) ==0 && strlen ($after _work) ==0) { $t ["Attendance_status"]= "4";  "4" stands for No attendance}//Reset normal commute time by attendance rule if (strlen ($go _work) = = 0 && strlen ($after _work) = = 0) {$go _work =
    "24:00"; $after _work = "00:00";//No attendance is calculated by strlen} if (_work) = = 0 && strlen ($after _work)!=0) {$t [Attendan Ce_go_worK "] = $go _work =" 08:35 ";/clock not clocked in 08:35 start calculation} if (strlen ($go _work)!= 0 && strlen ($after _work) = 0) { $t ["attendance_after_work"] = $after _work = "17:30";
   Work out for clocking in 17:30 calculate}//Calculate in time $minutes;//Save minutes $hours//Save small period $real _time1 = Explode (":", $go _work);//work time split array $real _time2 = Explode (":", $after _work);//work time split array/start processing in time $minutes =intval (intval ($real _time2[1))-intval ($real _t
    IME1[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)//start to select Strlen judge character length complement bit, now directly formatted output, the original structure unchanged.
     {$attendance _time = sprintf ("%02d", $hours). ":". sprintf ("%02d", $minutes);}
     else {$attendance _time = sprintf ("%02d", $hours). ":". sprintf ("%02d", $minutes);
    }//correction of the absence of time, the removal of the time of lunch in time if ($attendance _time== " -1:00") {$attendance _time= "00:00"; $t [attendance_time]= $attendance _time;//Save in Time $attendance->create ($t);//database}//Print_r ("Data import succeeded"). "
  <br> "; Raw CSV file data backup, files saved in the system's./uploadfiles/folder switch ($file _error) {case 0:echo "attendance data updated successfully." <br> ";
  Break Case 1:echo "The uploaded file exceeds the upload_max_filesize option limit in php.ini." <br> ";
  Break Case 2:echo "The size of the upload file exceeds the value specified by the Max_file_size option in the HTML form. "." <br> ";
  Break Case 3:echo "The file is only partially uploaded."
  <br> "; Case 4:echo "No files were uploaded".
  <br> "; Case 6:echo "Temp folder not found".
  <br> "; Case 7:echo "File write failed."
  <br> ";
}  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 ' original data backup succeeded ';
    else {echo ' Backs up raw data failed ';

 }}}?>

For more information about PHP interested readers can view the site topics: "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP Common database Operation tips Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.