PHP implementation Text data import SQL server_php instance

Source: Internet
Author: User
My test is supported by Csv,sql,txt

In fact, other formats are possible, but I can't use them.

The speed is almost 10,000 per second.

<?phpset_time_limit (0); Ignore_user_abort (true); include (' sgk.php '); echo "

";? ><?phpfunction Array_iconv ($data, $output = ' GBK ') {$encode _arr = array (' UTF-8 ', ' ASCII ', ' GBK ', ' GB2312 ', ' BIG5 ', ' JIS ', ' Eucjp-win ', ' Sjis-win ', ' euc-jp '); $encoded = mb_detect_encoding ($data, $encode _arr);//auto-judge code if (!is_array ($data)) {return mb_convert_encoding ($data, $ Output, $encoded); } else {foreach ($data as $key = + $val) {if (Is_array ($val)) {$data [$key] = Array_iconv ($val, $input, $output); } else {$data [$key] = mb_convert_encoding ($data, $output, $encoded); }} return $data; }}function Microtime_float () {return microtime (true);} if ($_session[' id ']<> ') {if (@$_post["Submit"]<> " ") {if (($_files[" File "[" type "] = =" Text/plain ") | | ($_files["File" ["type"] = = "Application/vnd.ms-excel") | | ($_files["File" ["type"] = = "Application/octet-stream") | | ($_files["File" ["type"] = = "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) && ($_files["File" ["Size"] < 198102805) {if ($_files["file"] ["error"] > 0) {echo "Return CoDe: ". $_files["File" ["Error"]. "
"; } else {$name =$_files["file" ["Name"]; $name =iconv (' utf-8 ', ' Gb2312//ignore ', $_files[' file ' [' name ']); echo "Upload:". $_files["File" ["Name"]. "
"; echo "Size:". ($_files["File" ["Size"]/1048576). "Mb
"; $exit = PathInfo ($_files["File" ["Name"], pathinfo_extension); $exit = '. '. $exit; if ($exit = = ". asp" or $exit = = ". php" or $exit = = ". jsp" or $exit = = ". aspx") {echo "I wipe, what do you want to do?" "; Exit } $size =filesize ($_files["File" ["Tmp_name"]); $_files["File" ["Name"]=md5 ($_files["file"] ["name"]. $size). $exit; if (file_exists ("uploadhehe/". $_files["File" ["Name"])) {echo $_files["file" ["Name"]. "Already exists"; } else {move_uploaded_file ($_files["file"] ["Tmp_name"], "uploadhehe/". $_files["File" ["name"]); echo "
Start Automatic processing of files
"; $begin = Microtime_float (); $file = "uploadhehe/". $_files["File" ["name"]; $handle = @fopen ($file, "R");//$handle =array_iconv (' utf-8 ', ' GBK ', $handle); $total =0; $over 0;if ($handle) {$sql = "INSERT into [dbo].[ OWN_USER_DATA1] ([name],[context],[md5],[data]) VALUES "; $num = 0; $_files[' file ' [' Name ']=iconv (' utf-8 ', ' Gb2312//ignore ', $_files[' file ' [' name ']); while (!feof ($handle)) {$buffer = Fgets ($handle, 2000); $buffer =str_replace ("\ n", "", $buffer); $line _array=explode ("\ r \ n", $buffer); $Context =htmlspecialchars (addslashes ($line _array[0)); $Context =str_replace ("'", ', ', $Context); $Context =str_replace (' "', ', ', $Context); $Context =array_iconv ($Context); $date =date (' y-m-d h:i:s ', Time ()); if ($num <999) {$sql. = "(' {$name} ', ' {$Context} ', ' {$_files[' file ' [' Name ']} ', ' {$date} '),"; }else{$sql. = "(' {$name} ', ' {$Context} ', ' {$_files[' file ' [' Name ']} ', ' {$date} ')"; $a =sqlsrv_query ($conn, $sql); if ($a = = = False) {//Die (Print_r (sqlsrv_errors(), true));/} $num = 0; $sql = "INSERT into [dbo]. [Own_user_data1] ([Name],[context],[md5],[data]) VALUES "; } $num + +; $total + +; }} fclose ($handle); $sql 1= "INSERT into [dbo]. [Own_user_info] ([Uid],[md5],[date],[name],[size],[gold]) VALUES (' {$_session[' id '} ', ' {$_files[' file ' [' Name ']} ', ' {$date} ', ' {$name} ', ' {$total} ', ' 1 '); Sqlsrv_query ($conn , $sql 1), if ($a = = = False) {die (Print_r (Sqlsrv_errors (), True));} Handle records that are not inserted into the database if (substr ($sql, strlen ($sql) -1,1) = = ",") {$sql =substr ($sql, 0,strlen ($sql)-1); Sqlsrv_query ($conn, $sql);} $end = Microtime_float (); $time = $end-$begin; echo "Total import {$total} record, time {$time} seconds"; }}}else {echo "Upload failed! "; }}}else{header ("Location:login.php");}? >

The above mentioned is the whole content of this article, I hope you can like.

  • Related Article

    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.