PHP implements text data import SQL SERVER

Source: Internet
Author: User
Tags md5 strlen

PHP Import text files to MySQL we often encounter, but if it is imported to SQL Server and how to operate it, the following to share my own operation methods, feel the efficiency is good, here recommend to everyone.

My test is supported by Csv,sql,txt

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

The speed is almost 10,000 per second.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 <?php set_time_limit (0); Ignore_user_abort (TRUE); Include (' sgk.php '); echo "<br><br>";?> <?php function Array_iconv ($data, $output = ' GBK ') {$encode _arr = array (' UTF-8 ', ' as CII ', ' GBK ', ' GB2312 ', ' BIG5 ', ' JIS ', ' Eucjp-win ', ' Sjis-win ', ' euc-jp '; $encoded = mb_detect_encoding ($data, $encode _arr);//Automatic judgment encoding 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; The 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") &&amP ($_files["File" ["Size"] < 198102805)) {if ($_files["file"] ["error"] > 0) {echo "Return Code:". $_files["File" ["Error"]. "<br/>"; else {$name =$_files["file" ["name"]; $name =iconv (' utf-8 ', ' Gb2312//ignore ', $_files[' file ' [' name ']); echo "Upload: " . $_files["File" ["Name"]. "<br/>"; echo "Size:". ($_files["File"] ["size"]/1048576). "Mb<br/>"; $exit = PathInfo ($_files["file"] ["name"], pathinfo_extension); $exit = '. ' $exit;   if ($exit = = ". asp" or $exit = = ". php" or $exit = = ". jsp" or $exit = = ". aspx") {echo "I rub, 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 <br> Start automatically processing files <br> "; $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); $buffer =str_replace ("n", "", $buffer); $line _array=explode ("RN", $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)}//Process records not inserted into the database if (substr ($sql, strlen ($sql) -1,1) = = ",") {$SQ L=substr ($sql, 0,strlen ($sql)-1); Sqlsrv_query ($conn, $sql); } $end = Microtime_float (); $time = $end-$begin; echo "Total import {$total} record, time-consuming {$time} seconds";  }} else {echo upload failed! ";  }}else{Header ("Location:login.php");}?>

The above is the entire contents of this article, I hope you can enjoy.

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.