/
user_upload.php
452 lines (359 loc) · 14.1 KB
/
user_upload.php
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
<?php
/*
###########################################################
# Author: Akmal Hossain
#
# Description: The PHP script inserts data from csv file
# to SQL database. The script takes command
# line arguments as directives. Each directive
# need to be followed by database username,
# password and host name which has the database.
# The script avoids insertion of duplication
# data and invalid email addresses.
###########################################################
*/
$servername = ""; // Global variaable for host name
$username = ""; // Global variable for SQL username
$password = ""; // Global variable for SQL password
$dbname = "DB"; // Global variable to hold the database name
/*
commandCheck() function takes the user argument
and checks which directive has been entered by user
*/
function commandCheck ($command){
global $servername, $username, $password;
// Swicth case checking input argument
switch ($command[1]){
case "--help":
helpDirectives();
break;
case "--file":
$filename = $command[2];
if($command[3] == "-u")
{
$username = $command[4];
}
elseif($command[3] == "-p")
{
$password = $command[4];
}
elseif($command[3] == "-h")
{
$servername = $command[4];
}
else
{
exit ("Invalid user or host argument\n");
}
if($command[5] == "-u")
{
$username = $command[6];
}
elseif($command[5] == "-p")
{
$password = $command[6];
}
elseif($command[5] == "-h")
{
$servername = $command[6];
}
else
{
exit ("Invalid user or host argument\n");
}
if($command[7] == "-u")
{
$username = $command[8];
}
elseif($command[7] == "-p")
{
$password = $command[8];
}
elseif($command[7] == "-h")
{
$servername = $command[8];
}
else
{
exit ("Invalid user or host argument\n");
}
initDB();
insertFile($filename);
break;
case "--create_table":
if($command[2] == "-u")
{
$username = $command[3];
}
elseif($command[2] == "-p")
{
$password = $command[3];
}
elseif($command[2] == "-h")
{
$servername = $command[3];
}
else
{
exit ("Invalid user or host argument\n");
}
if($command[4] == "-u")
{
$username = $command[5];
}
elseif($command[4] == "-p")
{
$password = $command[5];
}
elseif($command[4] == "-h")
{
$servername = $command[5];
}
else
{
exit ("Invalid user or host argument\n");
}
if($command[6] == "-u")
{
$username = $command[7];
}
elseif($command[6] == "-p")
{
$password = $command[7];
}
elseif($command[6] == "-h")
{
$servername = $command[7];
}
else
{
exit ("Invalid user or host argument\n");
}
initDB();
createTable();
break;
case "--dry_run":
$filename = $command[3];
if($command[4] == "-u")
{
$username = $command[5];
}
elseif($command[4] == "-p")
{
$password = $command[5];
}
elseif($command[4] == "-h")
{
$servername = $command[5];
}
else
{
exit ("Invalid user or host argument\n");
}
if($command[6] == "-u")
{
$username = $command[7];
}
elseif($command[6] == "-p")
{
$password = $command[7];
}
elseif($command[6] == "-h")
{
$servername = $command[7];
}
else
{
exit ("Invalid user or host argument\n");
}
if($command[8] == "-u")
{
$username = $command[9];
}
elseif($command[8] == "-p")
{
$password = $command[9];
}
elseif($command[8] == "-h")
{
$servername = $command[9];
}
else
{
exit ("Invalid user or host argument\n");
}
initDB();
dryRun($filename);
break;
default:
echo "Invalid command entered\n";
}
}
/*
helpDirectives() function is invoked when user enters --help.
It prints out definition of each command line argument option.
*/
function helpDirectives(){
echo "\n";
echo "--file [csv le name] – this is the name of the CSV to be parsed.\n\n";
echo "--create_table – this will cause the MySQL users table to be built (and no further action will be taken).\n\n";
echo "--dry_run – this will be used with the --file directive in the instance that we want to run the script but not insert into the DB. All other functions will be executed, but the database won't be altered.\n\n";
echo "-u – MySQL username.\n\n";
echo "-p – MySQL password.\n\n";
echo "-h – MySQL host.\n\n";
echo "--help – which will output the above list of directives with details.\n";
}
/*
dryRun() function is invoked when user enters --dry_run.
It checks data insertion to the database without commiting
the changes (Rollback).
*/
function dryRun($filename){
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error."\n\n");
}
echo "Connected successfully\n";
echo "Turning Auto Commit off for Dry Run\n";
$conn->autocommit(FALSE);
$file = fopen($filename,"r");
// Parsing the CSV file
while(! feof($file))
{
// Preprocessing data to meet standard before insertion
$line = (fgetcsv($file));
$firstName = str_replace('\'', '\'\'', (preg_replace('/\s+/', '', (ucwords(strtolower($line[0]))))));
$lastName = str_replace('\'', '\'\'', (preg_replace('/\s+/', '', (ucwords(strtolower($line[1]))))));
$emailAddress = str_replace('\'', '\'\'', (preg_replace('/\s+/', '', ($line[2]))));
// Checking email address validity
if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL))
{
echo "This ($emailAddress) email address is considered valid.\n";
$sql = "INSERT INTO users (name, surname, email)
VALUES ('$firstName', '$lastName', '$emailAddress')";
// Execute SQL statement
if ($conn->query($sql) === TRUE) {
echo "New record created successfully\n";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error . "\n\n";
}
// Rollback changes
$conn->rollback();
echo "New record insertion rolled back\n";
}
else
{
echo "($emailAddress) is not valid\n";
}
}
fclose($file);
$conn->autocommit(TRUE);
$conn->close();
}
/*
insertFile() function is invoked when --file directive in entered.
It parses the input CSV file and saved all valid data to the database
for a authorized user.
*/
function insertFile($filename){
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error."\n\n");
}
echo "Connected successfully\n";
echo "Inserting data from $filename to DB\n";
$file = fopen($filename,"r");
// Parsing the CSV file
while(! feof($file))
{
// Preprocessing data to meet standard before insertion
$line = (fgetcsv($file));
$firstName = str_replace('\'', '\'\'', (preg_replace('/\s+/', '', (ucwords(strtolower($line[0]))))));
$lastName = str_replace('\'', '\'\'', (preg_replace('/\s+/', '', (ucwords(strtolower($line[1]))))));
$emailAddress = str_replace('\'', '\'\'', (preg_replace('/\s+/', '', ($line[2]))));
// Checking email address validity
if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL))
{
echo "This ($emailAddress) email address is considered valid.\n";
$sql = "INSERT INTO users (name, surname, email)
VALUES ('$firstName', '$lastName', '$emailAddress')";
// Execute SQL statement
if ($conn->query($sql) === TRUE) {
echo "New record created successfully\n";
}
else {
echo "Error: " . $sql . "<br>" . $conn->error . "\n\n";
}
}
else
{
echo "($emailAddress) is not valid\n";
}
}
fclose($file);
$conn->close();
}
/*
initDB() function is invoked when any of the directive entered except --help.
It creates the database if it doesn't exists and checks connection.
*/
function initDB(){
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error."\n\n");
}
echo "Connected successfully\n";
// Create database
$sql = "CREATE DATABASE IF NOT EXISTS $dbname";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully\n\n";
}
else {
echo "Error creating database: " . $conn->error. "\n\n";
}
$conn->close();
}
/*
createTable() function is invoked when --create_table directive is entered.
It creates "users" table inside already created database in initDB().
*/
function createTable(){
global $servername, $username, $password, $dbname;
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error."\n\n");
}
echo "Connected successfully\n";
// SQL statement defining "users" table
$sql = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
surname VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
insert_date TIMESTAMP,
UNIQUE (email)
)";
// Creating table
if ($conn->query($sql) === TRUE) {
echo "Table users created successfully\n\n";
}
else {
echo "Error creating table: " . $conn->error . "\n\n";
}
$conn->close();
}
// Starting point of script
commandCheck($argv);
?>