


PHP implements the method of importing cvs data into MySQL based on the Fleaphp framework, fleaphpcvs_PHP tutorial
Jul 12, 2016 am 08:58 AMphp implements the method of importing cvs data into MySQL based on the Fleaphp framework, fleaphpcvs
This article describes the method of php based on the Fleaphp framework to implement the method of importing cvs data into MySQL. 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"); } //顯示錯(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ò),請(qǐng)檢查上傳文件。"; 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("文件命名格式不對(duì),正確格式。例: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開(kāi)始計(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ù)組 //開(kāi)始處理在勤時(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) //開(kāi)始選用strlen判斷字符長(zhǎng)度補(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ù)庫(kù) } // 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 "上傳的文件超過(guò)了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值."."<br>"; break; case 2: Echo "上傳文件的大小超過(guò)了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值。"."<br>"; break; case 3: Echo "文件只有部分被上傳"."<br>";break; case 4: Echo "沒(méi)有文件被上傳"."<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ù)失敗'; } } } } ?>
Readers who are interested in more PHP related content can check out the special topics of this site: "php object-oriented programming introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php mysql database operation introductory tutorial" Summary of common database operation skills in PHP》
I hope this article will be helpful to everyone in PHP programming.
Articles you may be interested in:
- Fleaphp rolesNameField bug solution
- How to use the find function of fleaphp crud operation
- fleaphp crud operation of findByField How to use functions
- Common methods of paging in fleaphp How to use Pager
- Security setting method of FleaPHP
- Ingenious solution to uncertain multi-condition query under fleaphp
- Example introduction of PHP's method of processing excel cvs table
- How to import excel file into mysql database through PHP
- PHP export MySQL data to Excel file (fputcsv)
- PHP How to import Excel into MySQL
- Graphic method of importing data from Excel into MySql under phpMyAdmin
- php Import excel into mysql

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

CTE is a temporary result set in MySQL used to simplify complex queries. It can be referenced multiple times in the current query, improving code readability and maintenance. For example, when looking for the latest orders for each user in the orders table, you can first obtain the latest order date for each user through the CTE, and then associate it with the original table to obtain the complete record. Compared with subqueries, the CTE structure is clearer and the logic is easier to debug. Usage tips include explicit alias, concatenating multiple CTEs, and processing tree data with recursive CTEs. Mastering CTE can make SQL more elegant and efficient.

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

There are three key ways to avoid the "undefinedindex" error: First, use isset() to check whether the array key exists and ensure that the value is not null, which is suitable for most common scenarios; second, use array_key_exists() to only determine whether the key exists, which is suitable for situations where the key does not exist and the value is null; finally, use the empty merge operator?? (PHP7) to concisely set the default value, which is recommended for modern PHP projects, and pay attention to the spelling of form field names, use extract() carefully, and check the array is not empty before traversing to further avoid risks.

When using PHP preprocessing statements to execute queries with IN clauses, 1. Dynamically generate placeholders according to the length of the array; 2. When using PDO, you can directly pass in the array, and use array_values to ensure continuous indexes; 3. When using mysqli, you need to construct type strings and bind parameters, pay attention to the way of expanding the array and version compatibility; 4. Avoid splicing SQL, processing empty arrays, and ensuring data types match. The specific method is: first use implode and array_fill to generate placeholders, and then bind parameters according to the extended characteristics to safely execute IN queries.

WhensettingupMySQLtables,choosingtherightdatatypesiscrucialforefficiencyandscalability.1)Understandthedataeachcolumnwillstore—numbers,text,dates,orflags—andchooseaccordingly.2)UseCHARforfixed-lengthdatalikecountrycodesandVARCHARforvariable-lengthdata

Reasons and solutions for the header function jump failure: 1. There is output before the header, and all pre-outputs need to be checked and removed or ob_start() buffer is used; 2. The failure to add exit causes subsequent code interference, and exit or die should be added immediately after the jump; 3. The path error should be used to ensure correctness by using absolute paths or dynamic splicing; 4. Server configuration or cache interference can be tried to clear the cache or replace the environment test.
