php實現文本資料匯入SQL SERVER_PHP教程

來源:互聯網
上載者:User

php實現文本資料匯入SQL SERVER


  php將文字檔匯入mysql我們經常遇到,但是如果是匯入到sqlserver又應該如何操作呢,下面就給大家分享一下本人的操作方法,感覺效率還不錯,這裡推薦給大家。

  我測試是支援的CSV,SQL,TXT

  其實其他格式也可以,只不過我用不到而已

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

  ?

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

43

44

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

84

85

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

119

120

121

122

123

124

125

126

set_time_limit(0);

ignore_user_abort(true);

include('sgk.php');

echo "

";

?>

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"] . "
";

}

else

{

$name=$_FILES["file"]["name"];

$name=iconv('utf-8', 'GB2312//IGNORE', $_FILES['file']['name']);

echo "Upload: " . $_FILES["file"]["name"] . "
";

echo "Size: " . ($_FILES["file"]["size"] / 1048576) . " Mb
";

$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 "
開始自動處理檔案
";

$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");

}

?>

  以上所述就是本文的全部內容了,希望大家能夠喜歡。

http://www.bkjia.com/PHPjc/1000067.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1000067.htmlTechArticlephp實現文本資料匯入SQL SERVER php將文字檔匯入mysql我們經常遇到,但是如果是匯入到sqlserver又應該如何操作呢,下面就給大家分享一下本人...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.