


PHP implements the method of importing cvs data into MySQL based on the Fleaphp framework
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']));//获取扩展名 $extenti/.*/.(.*[^/.].*)*/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: "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP String Usage" ", "Introduction Tutorial on PHP+MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.
The above introduces the method of importing cvs data into MySQL based on the Fleaphp framework in PHP, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

How to delete a file or directory using File.delete() method in Java? Overview: In Java, we can delete a file or directory using the delete() method of the File class. This method is used to delete the specified file or directory. However, it should be noted that this method can only delete empty directories or files that are not opened by other programs. If file or directory deletion fails, you can find the specific reason by catching IOException. Step 1: Import related packages First, we need

Create multi-level directories using Java's File.mkdirs() function In Java, we often need to create folders to store and organize files. Sometimes, we need to create multi-level directories, which are folders containing subfolders. Java provides the mkdirs() function of the File class to implement this function. The File class is a class in Java that handles files and directories. It provides a series of methods for operating files and directories. Among them, the mkdirs() function is a function that creates multi-level directories. Down
