Php implements the cvs data import MySQL Method Based on the Fleaphp framework. fleaphpcvs
This example describes how php imports cvs data into MySQL based on the Fleaphp framework. We will share this with you 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");} // display Error pri Vate function showErro () {error_reporting (E_ALL); ini_set ("display_errors", "On") ;}// judge the date private function isDate ($ file_name) {$ filename = explode ('. ', $ file_name); $ real_name = $ filename [count ($ filename)-2]; // get the file name $ 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 if ($ file_name = null) {echo "file selection error, check whether the file is uploaded. "; Exit;} // determine the file size if ($ file_size> = 10241024) {$ file_size = round ($ file_size/1048576*100)/100. 'mb '; print_r ("the size of the uploaded file is ". "$ file_size"); echo ": the system can only upload files smaller than 10 MB. "; Exit;} // $ extention_name = end (explode ('. ', $ _ FILES ["upload_file"] ['name']); // obtain the extension $ extention_name = preg_replace ('/. */. (. * [^/.]. *) */iU ',' // 1', $ file_name); // obtain the file extension. // check the file type if ($ file_type! = "Application/vnd. ms-excel" & $ extention_name! = "Csv") {echo "type of the file you uploaded :. ", $ extention_name," <br> "; print_r (" allowed file types :. csv "); exit;} if (file_exists ($ this-> uploaddir. $ _ FILES ['upload _ file'] ['name']) {print ("data with the same name in the backup directory exists"); // exit exists in the file ;} if ($ this-> isDate ($ file_name) = FALSE) {print ("the file name format is incorrect. The format is correct. Example: 2010-10-28.csv "); // The file has exit;} // var_dump (file_exists ($ this-> uploaddir. $ _ FILES ['upload _ file'] ['name']); // test returns print_r ("original attendance 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 _ atte Ndance '); // print_r ($ data); // data import processing for ($ I = 1; $ I <count ($ data)-1; $ I ++) {$ a = explode (";", $ data [$ I]); // convert the encoding format $ 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 ($ ); $ t = array (); $ t ["ATTENDANCE_FINGERPRINT_ID"] = intval (trim ($ a [0], "/" "); $ t [" ATTENDA NCE_USER_NAME "] = trim ($ a [1],"/""); $ t ["ATTENDANCE_DATE"] = trim ($ a [2], "/" "); // $ t ["ATTENDANCE_DIVISIONS"] = trim ($ a [3], "/" "); // Delete the field ATTENDANCE_DIVISIONS In the table $ t ["ATTENDANCE_GO_WORK"] = trim ($ a [4], "/" "); $ t ["ATTENDANCE_AFTER_WORK"] = trim ($ a [5], "/" "); // determine the punching condition $ go_work = trim ($ a [4], "/" "); // start time $ after_work = trim ($ a [5],"/""); // End Time // print_r (var_dump ($ go_work); // exit (); if (strlen ($ go_work) = 0 & strlen ($ After_work )! = 0) {$ t ["ATTENDANCE_STATUS"] = "1"; // "1" indicates normal attendance} if (strlen ($ go_work )! = 0 & strlen ($ after_work) = 0) {$ t ["ATTENDANCE_STATUS"] = "2 "; // "2" indicates punching at work.} if (strlen ($ go_work )! = 0 & strlen ($ after_work )! = 0) {$ t ["ATTENDANCE_STATUS"] = "3"; // "3" indicates not playing after work} if (strlen ($ go_work) = 0 & strlen ($ after_work) = 0) {$ t ["ATTENDANCE_STATUS"] = "4 "; // "4" indicates no attendance} // reset normal commuting time according to attendance rules if (strlen ($ go_work) = 0 & strlen ($ after_work) = 0) {$ go_work = "24:00"; $ after_work = "00:00"; // calculate when no attendance is received} if (strlen ($ go_work) = 0 & strlen ($ after_work )! = 0) {$ t ["ATTENDANCE_GO_WORK"] = $ go_work = ""; // if ($ go_work), the task starts )! = 0 & strlen ($ after_work) = 0) {$ t ["ATTENDANCE_AFTER_WORK"] = $ after_work = "17:30 "; // calculate the on-duty time by when the service is closed.} // calculate the on-duty time $ minutes; // Save the minute segment $ hours; // Save the small period of time $ real_time1 = explode (":", $ go_work); // the array of working hours. $ real_time2 = explode (":", $ after_work ); // split the array during off-duty time // start processing the on-duty time $ minutes = intval ($ real_time2 [1])-intval ($ real_time1 [1]); if ($ minutes <0) {$ hours = intval ($ real_time2 [0])-1)-intval ($ real_time1 [0]); If ($ hours <= 0) {$ hours = intval ($ real_time2 [0])-1)-intval ($ real_time1 [0]) + 24 );} $ minutes = intval ($ real_time2 [1]) + 60-intval ($ real_time1 [1]); $ attendance_time = sprintf ("% 02d", $ hours ). ":". sprintf ("% 02d", $ minutes);} else {$ hours = intval ($ real_time2 [0])-intval ($ real_time1 [0]); if ($ hours <= 0) {$ hours = intval ($ real_time2 [0])-intval ($ real_time1 [0]) + 24 );} if ($ minutes> = 1 0 & $ minutes <60) // start to use strlen to determine whether the character length is equal to or equal to the character length. Now, the output is formatted directly, and the original structure remains unchanged. {$ Attendance_time = sprintf ("% 02d", $ hours ). ":". sprintf ("% 02d", $ minutes);} else {$ attendance_time = sprintf ("% 02d", $ hours ). ":". sprintf ("% 02d", $ minutes) ;}// correct the non-attendance time, remove the on-duty time during lunch break if ($ attendance_time = ") {$ attendance_time = "00:00";} $ t ["ATTENDANCE_TIME"] = $ attendance_time; // save on-duty time $ attendance-> create ($ t ); // save to database} // print_r ("data imported successfully "). "<br>"; // original csv file data backup, which is stored in the system. /uploadfiles/swi under the folder Tch ($ file_error) {case 0: Echo "attendance data updated successfully ". "<br>"; break; case 1: Echo "the uploaded file exceeds that of php. the value of the upload_max_filesize option in ini. ". "<br>"; break; case 2: Echo "the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. ". "<Br>"; break; case 3: Echo "only part of the file is uploaded ". "<br>"; break; case 4: Echo "no file is uploaded ". "<br>"; break; case 6: Echo "Temporary Folder not found ". "<br>"; break; case 7: Echo "file writing failed ". "<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_u Ploaded_file ($ file_tmp_name, $ this-> uploaddir. $ _ FILES ['upload _ file'] ['name']) {echo 'success of original data backup ';} else {echo 'failed to back up raw data ';}}}?>