当前位置: 移动技术网 > IT编程>开发语言>PHP > php实现文本数据导入SQL SERVER

php实现文本数据导入SQL SERVER

2018年06月22日  | 移动技术网IT编程  | 我要评论

我测试是支持的csv,sql,txt

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

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

<?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','ascii','gbk','gb2312','big5','jis','eucjp-win','sjis-win','euc-jp');
 $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['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"] . "<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 "我擦,你想做什么?";
  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('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);
$sql1="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,$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"); 
}
?>

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网