Home Backend Development PHP Tutorial php实现文本数据导入SQL SERVER_php实例

php实现文本数据导入SQL SERVER_php实例

Jun 07, 2016 pm 05:12 PM

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

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

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

<&#63;php
set_time_limit(0);
ignore_user_abort(true);
include('sgk.php');
echo "<br><br>";
&#63;>
<&#63;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"); 
}
&#63;>
Copy after login

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

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

See all articles