-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathupload.php
More file actions
78 lines (70 loc) · 2.13 KB
/
upload.php
File metadata and controls
78 lines (70 loc) · 2.13 KB
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
<?php
$csv = array();
$insert_data = array();
$i=0;
$row = 0;
$colName = array();
$tableName = $_POST['table'];
//read file
// check there are no errors
if($_FILES['csv']['error'] == 0){
$name = $_FILES['csv']['name'];
$ext = strtolower(end(explode('.', $_FILES['csv']['name'])));
$type = $_FILES['csv']['type'];
$tmpName = $_FILES['csv']['tmp_name'];
// check the file is a csv
if($ext === 'csv'){
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
set_time_limit(0);
$row = 0;
while(($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// number of fields in the csv
$col_count = count($data);
//echo($col_count);
// get the values from the csv
for($i=0 ; $i<$col_count ;$i++)
{
$csv[$row][$i] = $data[$i];
echo($csv[$row][$i]);
echo(" ");
}
echo "<br>";
// inc the row
$row++;
}
fclose($handle);
}
}
}
//keep in SQL
$con=mysqli_connect("127.0.0.1","root","","project_test");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
for($j=0;$j<$col_count;$j++){
// escape variables for security
$colName[$j] = mysqli_real_escape_string($con, $csv[0][$j]);
}
for($i=1;$i<$row;$i++){
for($j=0;$j<$col_count;$j++){
// escape variables for security
$insert_data[$j] = mysqli_real_escape_string($con, $csv[$i][$j]);
}
$sql="INSERT INTO ".$tableName."(";
for($j=0;$j<$col_count-1;$j++){
$sql .= $colName[$j].",";
}
$sql = $sql.$colName[$j].")VALUES (";
for($j=0;$j<$col_count-1;$j++){
$sql = $sql."'".$insert_data[$j]."',";
}
$sql = $sql."'".$insert_data[$j]."')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
echo "records added";
mysqli_close($con);
?>