Rumah >pembangunan bahagian belakang >tutorial php >php实现文本数据导入SQL SERVER

php实现文本数据导入SQL SERVER

高洛峰
高洛峰asal
2017-01-06 14:22:011228semak imbas

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

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

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

<?php
set_time_limit(0);
ignore_user_abort(true);
include(&#39;sgk.php&#39;);
echo "<br><br>";
?>
<?php
function array_iconv($data,$output = &#39;GBK&#39;) {
 $encode_arr = array(&#39;UTF-8&#39;,&#39;ASCII&#39;,&#39;GBK&#39;,&#39;GB2312&#39;,&#39;BIG5&#39;,&#39;JIS&#39;,&#39;eucjp-win&#39;,&#39;sjis-win&#39;,&#39;EUC-JP&#39;);
 $encoded = mb_detect_encoding($data, $encode_arr);//自动判断编码
 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[&#39;id&#39;]<>&#39;&#39;){
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"] . "<br />";
  }
 else
  {
  $name=$_FILES["file"]["name"];
  $name=iconv(&#39;utf-8&#39;, &#39;GB2312//IGNORE&#39;, $_FILES[&#39;file&#39;][&#39;name&#39;]); 
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1048576) . " Mb<br />";
  $exit= pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
  $exit=&#39;.&#39; .$exit;
  
    if ($exit==".asp" or $exit==".php" or $exit==".jsp" OR $exit==".aspx"){
  echo "我擦,你想做什么?";
  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"] . " 已经存在 ";
   }
  else
   {
   move_uploaded_file($_FILES["file"]["tmp_name"],
   "uploadhehe/" . $_FILES["file"]["name"]);
   echo "<br>开始自动处理文件<br>";
   $begin = microtime_float();
$file="uploadhehe/" . $_FILES["file"]["name"];
$handle = @fopen($file, "r");
//$handle=array_iconv(&#39;utf-8&#39;,&#39;gbk&#39;,$handle);
$total=0;
$over=0;
if ($handle) {
  $sql="INSERT INTO [dbo].[own_user_data1] ([Name],[Context],[md5],[data]) VALUES";
  $num=0;
  $_FILES[&#39;file&#39;][&#39;name&#39;]=iconv(&#39;utf-8&#39;, &#39;GB2312//IGNORE&#39;, $_FILES[&#39;file&#39;][&#39;name&#39;]); 
  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("&#39;", &#39;,&#39;, $Context);
    $Context=str_replace(&#39;"&#39;, &#39;,&#39;, $Context);
    $Context=array_iconv($Context);
    $date=date(&#39;y-m-d h:i:s&#39;,time());
    if($num<999){
    $sql.="(&#39;{$name}&#39;,&#39;{$Context}&#39;,&#39;{$_FILES[&#39;file&#39;][&#39;name&#39;]}&#39;,&#39;{$date}&#39;),"; 
    }else{
    $sql.="(&#39;{$name}&#39;,&#39;{$Context}&#39;,&#39;{$_FILES[&#39;file&#39;][&#39;name&#39;]}&#39;,&#39;{$date}&#39;)"; 
    $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);
$sql1="INSERT INTO [dbo].[own_user_info] ([uid],[md5],[date],[name],[size],[gold]) VALUES (&#39;{$_SESSION["id"]}&#39;,&#39;{$_FILES["file"]["name"]}&#39;,&#39;{$date}&#39;,&#39;{$name}&#39;,&#39;{$total}&#39;,&#39;1&#39;)";
sqlsrv_query($conn,$sql1);
if( $a === false ) {
   die( print_r( sqlsrv_errors(), true));
}
//处理没有插入到数据库的记录
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}条记录,耗时{$time}秒";
  
   }
  }
 }
else
 {
 echo "上传失败!";
  
 }
}
}else{
 header("Location: login.php"); 
}
?>

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

更多php实现文本数据导入SQL SERVER相关文章请关注PHP中文网!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn