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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
<?php //include 'config.php'; require_once("configure.php"); // Check if the user is logged in require_once( realpath(dirname(__FILE__)) . '/../common/config.php'); require_once( realpath(dirname(__FILE__)) . '/function_is_login.php'); if (!is_login()) { header("Location: index.php"); exit; }
// check file extension $file_name = $_FILES['clientdatafile']['name']; $file_ext = pathinfo($file_name, PATHINFO_EXTENSION); if (strtolower($file_ext) != 'csv') { header("Location: upload_clientdataform.php?msg=Not a csv file"); exit; }
function fgetcsv_utf8(&$handle, $length = null, $d = ",", $e = '"') { $d = preg_quote($d); $e = preg_quote($e); $_line = ""; $eof=false; while ($eof != true) { $_line .= (empty ($length) ? fgets($handle) : fgets($handle, $length)); $itemcnt = preg_match_all('/' . $e . '/', $_line, $dummy); if ($itemcnt % 2 == 0) $eof = true; } $_csv_line = preg_replace('/(?: |[ ])?$/', $d, trim($_line)); $_csv_pattern = '/(' . $e . '[^' . $e . ']*(?:' . $e . $e . '[^' . $e . ']*)*' . $e . '|[^' . $d . ']*)' . $d . '/'; preg_match_all($_csv_pattern, $_csv_line, $_csv_matches); $_csv_data = $_csv_matches[1]; for ($_csv_i = 0; $_csv_i < count($_csv_data); $_csv_i++) { $_csv_data[$_csv_i] = preg_replace("/^" . $e . "(.*)" . $e . "$/s", "$1", $_csv_data[$_csv_i]); $_csv_data[$_csv_i] = str_replace($e . $e, $e, $_csv_data[$_csv_i]); } return empty ($_line) ? false : $_csv_data; }
if (strlen($_FILES['clientdatafile']['name'])) { if (($handle = fopen($_FILES['clientdatafile']['tmp_name'], "r")) !== false) { $lineNumber = 0; while (($data = fgetcsv_utf8($handle, 1000, ",")) !== false) { $lineNumber++; if ($lineNumber == 1) { // Filter UTF-8 BOM $utf8bom = "\xEF\xBB\xBF"; if (strpos($data['0'], $utf8bom) === 0) $data['0'] = substr($data['0'], strlen($utf8bom)); } $i = 0; $customer_no = mysql_real_escape_string($data[strval($i++)]); $login = $customer_no; $company = mysql_real_escape_string($data[strval($i++)]); $address = mysql_real_escape_string($data[strval($i++)]); $contact_person = mysql_real_escape_string($data[strval($i++)]); $telValue = $data[strval($i++)]; $tel = mysql_real_escape_string($telValue); $fax = mysql_real_escape_string($data[strval($i++)]); $email = mysql_real_escape_string($data[strval($i++)]); $pwValue = $data[strval($i++)]; // $status = mysql_real_escape_string($data[strval($i++)]); $status = '1'; if (!empty($pwValue)) $pw = md5($pwValue); // override tel password if length > 0 else $pw = md5($telValue); // tel $cmsloginid = mysql_real_escape_string($_SESSION['cmsloginid']); if($customer_no) { if ($lineNumber == 1) { // // Delete all // $sql = "DELETE FROM clients"; // if (!mysql_query($sql)) { // fclose($handle); // echo 'Import Error:<br />'. mysql_error() .'<br />SQL: '. $sql; // exit; // } // // Reset auto increment // $sql = "ALTER TABLE clients AUTO_INCREMENT = 1"; // if (!mysql_query($sql)) { // fclose($handle); // echo 'Import Error:<br />'. mysql_error() .'<br />SQL: '. $sql; // exit; // } // Update all $sql = "UPDATE clients SET cstatus = '0' WHERE cstatus = '1'"; if (!mysql_query($sql)) { fclose($handle); echo 'Import Error:<br />'. mysql_error() .'<br />SQL: '. $sql; exit; } } $pwSql = !empty($pwValue) ? "cpw = '$pw', " : ""; $sql = " update clients set ccompany = '$company', caddress = '$address', ccontactp = '$contact_person', ctel = '$tel', cfax = '$fax', cmail = '$email', $pwSql cstatus = '$status', modifyday = NOW(), cmsloginid = '$cmsloginid' where cno = '$customer_no' limit 1 "; if(!mysql_query($sql)) { fclose($handle); echo 'Import Error:<br />'. mysql_error() .'<br />SQL: '. $sql; exit; } if (mysql_affected_rows() === 0) { $sql = " insert into clients (cno, ccompany, caddress, ccontactp, ctel, cfax, cmail, clogin, cpw, cstatus, creationday, modifyday, cmsloginid) value ('$customer_no', '$company', '$address', '$contact_person', '$tel', '$fax', '$email', '$login', '$pw', '$status', NOW(), NOW(), '$cmsloginid') "; if(!mysql_query($sql)) { fclose($handle); echo 'Import Error:<br />'. mysql_error() .'<br />SQL: '. $sql; exit; } } } } fclose($handle); } }
header("Location: upload_clientdataform.php?msg=Modify Successful");
|