-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
161 lines (135 loc) · 5.58 KB
/
index.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
<?php
include "config.php";
?>
<!DOCTYPE html>
<html>
<head>
<title>Batman</title>
<link rel="stylesheet" href="file.css">
</head>
<body>
<h1>Batman Server</h1>
<h2>Upload the CSV data to database</h2>
<h3>
<form method='post' action='' enctype="multipart/form-data">
<label for="myfile">Select a file:</label>
<input type="file" name="importfile"><br><br>
<h4><input type="submit" name="but_import" value='Start'></h4>
</form>
</h3>
<div id="progress" style="width:480px;border:1px solid #ccc;"></div>
<div id="information" style="width"></div>
<?php
if(isset($_POST['but_import'])){
if (isset($_FILES['importfile']['error']) && $_FILES['importfile']['error'] == 4) {
echo '<script type="text/javascript">';
echo ' alert("Please select the file")';
echo '</script>';
}
else{
$target_dir = "uploads/";
$target_file = $target_dir.basename($_FILES['importfile']['name']);
$filename=$_FILES['importfile']['name'];
$table = basename($filename,".csv");
$records = file($filename);
$noofrec = count($records);
$pass = -1;
$percent =0;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$uploadOk = 1;
if($imageFileType != 'csv'){
$uploadOk = 0;
}
if($uploadOk != 0){
if(move_uploaded_file($_FILES['importfile']['tmp_name'],$target_file)){
$fileexists = 0;
if(file_exists($target_file)){
$fileexists=1;
}
if($fileexists == 1){
$file = fopen($target_file,"r");
$index=0;
$importData_arr = array();
while (($data = fgetcsv($file,1000,",")) != FALSE) {
$num = count($data);
for($c=0;$c<$num;$c++){
$importData_arr[$index][]=$data[$c];
}
$index++;
}
fclose($file);
$skip = 0;
$dat=[];
$str='';
foreach($importData_arr as $data){
if($skip == 0){
for($c=0;$c<$num;$c++){
$new = array_push($dat,$data[$c]);
if($data[$c]=='password'){
$pass = $c;
}
}
}
if($skip != 0){
for($c=0;$c<$num;$c++){
$str1="`$dat[$c]` varchar(50) NOT NULL, ";
$str=$str.$str1;
}
}
if($skip == 1){
break;
}
$skip++;
}
$str=substr_replace($str,"",-2);
$ntable = "CREATE TABLE $table ($str)";
if ($con->query($ntable) == TRUE) {
echo "";
}
$skip = 0;
foreach($importData_arr as $data){
if($skip != 0){
$val="";
for($c=0;$c<$num;$c++){
if($pass == $c){
$data[$c] = md5($data[$c]);
$val = $val."'$data[$c]',";
}
else
$val = $val."'$data[$c]',";
}
$val=substr_replace($val,"",-1);
$res = "insert into $table values($val)";
mysqli_query($con,$res);
$percent = intval($skip/($noofrec-1) * 100)."%";
}
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\"> </div>";
document.getElementById("information").innerHTML="'.$skip.' row(s) processed.";
</script>';
flush();
$skip++;
}
if(file_exists($target_file)){
unlink($target_file);
}
if($skip == $noofrec){
echo '<script type="text/javascript">';
echo ' alert("All records in CSV file is uploaded to Database successfully")';
echo '</script>';
}
else{
$st=strval($skip);
$st=$st." number of records in CSV file is uploaded to Database";
echo '<script type="text/javascript">';
echo ' alert('.$st.')';
echo '</script>';
}
}
}
}
}
}
?>
</body>
</html>