当前位置:首页 > PHP教程 > php应用 > 列表

php实现文本数据导入SQL SERVER

发布:smiling 来源: PHP粉丝网  添加日期:2021-05-26 22:10:12 浏览: 评论:0 

php将文本文件导入mysql我们经常遇到,但是如果是导入到sqlserver又应该如何操作呢,下面就给大家分享一下本人的操作方法,感觉效率还不错,这里推荐给大家。

我测试是支持的CSV,SQL,TXT

其实其他格式也可以,只不过我用不到而已

速度是差不多是每秒一万吧

  1. <?php 
  2. set_time_limit(0); 
  3. ignore_user_abort(true); 
  4. include('sgk.php'); 
  5. echo "<br><br>"; 
  6. ?> 
  7. <?php 
  8. function array_iconv($data,$output = 'GBK') { 
  9.  $encode_arr = array('UTF-8','ASCII','GBK','GB2312','BIG5','JIS','eucjp-win','sjis-win','EUC-JP'); 
  10.  $encoded = mb_detect_encoding($data, $encode_arr);//自动判断编码 
  11.  if (!is_array($data)) { 
  12.  return mb_convert_encoding($data, $output, $encoded); 
  13.  } 
  14.  else { 
  15.  foreach ($data as $key=>$val) { 
  16.   if(is_array($val)) { 
  17.   $data[$key] = array_iconv($val, $input, $output); 
  18.   } else { 
  19.   $data[$key] = mb_convert_encoding($data, $output, $encoded); 
  20.   } 
  21.  } 
  22.  return $data; 
  23.  } 
  24. } 
  25. function microtime_float(){  
  26.   return microtime(true);  
  27. }  
  28. if ($_SESSION['id']<>''){ 
  29. if (@$_POST["submit"]<>""){ 
  30. if ((($_FILES["file"]["type"] == "text/plain") 
  31. || ($_FILES["file"]["type"] == "application/vnd.ms-excel") || ($_FILES["file"]["type"] == "application/octet-stream") 
  32. || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")) 
  33. && ($_FILES["file"]["size"] < 198102805)) 
  34.  { 
  35.  if ($_FILES["file"]["error"] > 0) 
  36.   { 
  37.   echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; 
  38.   } 
  39.  else 
  40.   { 
  41.   $name=$_FILES["file"]["name"]; 
  42.   $name=iconv('utf-8', 'GB2312//IGNORE', $_FILES['file']['name']);  
  43.   echo "Upload: " . $_FILES["file"]["name"] . "<br />"; 
  44.   echo "Size: " . ($_FILES["file"]["size"] / 1048576) . " Mb<br />"; 
  45.   $exit= pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION); 
  46.   $exit='.' .$exit; 
  47.    
  48.     if ($exit==".asp" or $exit==".php" or $exit==".jsp" OR $exit==".aspx"){ 
  49.   echo "我擦,你想做什么?"; 
  50.   exit; 
  51.   } 
  52.   $size=filesize($_FILES["file"]["tmp_name"]); 
  53.    $_FILES["file"]["name"]=md5($_FILES["file"]["name"].$size).$exit; 
  54.   if (file_exists("uploadhehe/" . $_FILES["file"]["name"])) 
  55.    { 
  56.    echo $_FILES["file"]["name"] . " 已经存在 "; 
  57.    } 
  58.   else 
  59.    { 
  60.    move_uploaded_file($_FILES["file"]["tmp_name"], 
  61.    "uploadhehe/" . $_FILES["file"]["name"]); 
  62.    echo "<br>开始自动处理文件<br>"; 
  63.    $begin = microtime_float(); 
  64. $file="uploadhehe/" . $_FILES["file"]["name"]; 
  65. $handle = @fopen($file, "r"); 
  66. //$handle=array_iconv('utf-8','gbk',$handle); 
  67. $total=0; 
  68. $over=0; 
  69. if ($handle) { 
  70.   $sql="INSERT INTO [dbo].[own_user_data1] ([Name],[Context],[md5],[data]) VALUES"; 
  71.   $num=0; 
  72.   $_FILES['file']['name']=iconv('utf-8', 'GB2312//IGNORE', $_FILES['file']['name']);  
  73.   while (!feof($handle)) { 
  74.     $buffer = fgets($handle, 2000); 
  75.     $buffer=str_replace("\n", "", $buffer); 
  76.     $line_array=explode("\r\n", $buffer); 
  77.     $Context=htmlspecialchars(addslashes($line_array[0])); 
  78.     $Context=str_replace("'", ',', $Context); 
  79.     $Context=str_replace('"', ',', $Context); 
  80.     $Context=array_iconv($Context); 
  81.     $date=date('y-m-d h:i:s',time()); 
  82.     if($num<999){ 
  83.     $sql.="('{$name}','{$Context}','{$_FILES['file']['name']}','{$date}'),";  
  84.     }else{ 
  85.     $sql.="('{$name}','{$Context}','{$_FILES['file']['name']}','{$date}')";  
  86.     $a=sqlsrv_query($conn,$sql); 
  87.     // if( $a === false ) { 
  88.    // die( print_r( sqlsrv_errors(), true)); 
  89. // } 
  90.     $num=0; 
  91.     $sql="INSERT INTO [dbo].[own_user_data1] ([Name],[Context],[md5],[data]) VALUES"; 
  92.    
  93.     }    
  94.     $num++;  
  95.     $total++; 
  96.    
  97.    } 
  98.  }  
  99. fclose($handle); 
  100. $sql1="INSERT INTO [dbo].[own_user_info] ([uid],[md5],[date],[name],[size],[gold]) VALUES ('{$_SESSION["id"]}','{$_FILES["file"]["name"]}','{$date}','{$name}','{$total}','1')"; 
  101. sqlsrv_query($conn,$sql1); 
  102. if( $a === false ) { 
  103.    die( print_r( sqlsrv_errors(), true)); 
  104. } 
  105. //处理没有插入到数据库的记录 
  106. if(substr($sql, strlen($sql)-1,1)==","){ 
  107.   $sql=substr($sql, 0,strlen($sql)-1); 
  108.   sqlsrv_query($conn,$sql); 
  109. } 
  110. $end = microtime_float(); 
  111. $time = $end-$begin; 
  112. echo "共导入{$total}条记录,耗时{$time}秒"; 
  113.    
  114.    } 
  115.   } 
  116.  } 
  117. else 
  118.  { 
  119.  echo "上传失败!"; 
  120.    
  121.  } 
  122. } 
  123. }else{ 
  124.  header("Location: login.php");  
  125. } 
  126. ?> 

以上所述就是本文的全部内容了,希望大家能够喜欢。

Tags: php文本数据导入SQL

分享到: