-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
90 lines (78 loc) · 2.87 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
<html>
<head></head>
<body>
<div style="border: 1px solid black;">
<center>
<?php
session_start();
set_time_limit(0);
require 'src/XBase/Table.php';
require 'src/XBase/Column.php';
require 'src/XBase/Record.php';
require 'src/XBase/Memo.php';
use XBase\Table;
$mysqli = new mysqli("localhost", "root", "","dbname");
if(isset($_FILES['dbfFile']['tmp_name'])) {
$_SESSION['dbfPath'] = "dbf/".time()."_".$_FILES['dbfFile']['name'];
move_uploaded_file($_FILES['dbfFile']['tmp_name'], $_SESSION['dbfPath']);
$dbf = new Table($_SESSION['dbfPath']);
$dbfColumns = $dbf->getColumns();
$_SESSION['destinationTable'] = $_POST['destinationTable'];
$result = $mysqli->query("SELECT * FROM ".$_POST['destinationTable']." LIMIT 1 ");
$tableFields = $result->fetch_fields();
echo '<form method="POST" action="index.php"><table>';
echo "<tr><td>".$_FILES['dbfFile']['name']."</td><td>".$_POST['destinationTable']."</td></tr>";
foreach($dbfColumns as $col) {
echo "<tr><td>".$col->name."</td><td><select name=\"Columns[".$col->name."]\"><option>Ignore</option>";
foreach($tableFields as $tableField)
echo "<option value=\"".$tableField->name."\">".$tableField->name."</option>";
echo "</select></td></tr>";
}
echo "</table><br/><br/>";
?>
<input type="submit" value="Start Import" style="width: 200px;"/>
</form>
<?php
} elseif(isset($_POST['Columns'])) {
$dbf = new Table($_SESSION['dbfPath']);
$table = $_SESSION['destinationTable'];
$columns = $_POST['Columns'];
foreach ($columns as $dbfColumn =>$tableColumn)
if($tableColumn == "Ignore")
unset($columns[$dbfColumn]);
$inserted = 0;
while($record = $dbf->nextRecord())
{
$recordValues = [];
foreach (array_keys($columns) as $dbfColumn) {
$recordValues[] = "'".mysqli_escape_string($mysqli,$record->forceGetString($dbfColumn))."'";
}
$query = "INSERT INTO ".$table." (".implode(',',array_values($columns)).") VALUES(".implode(",",$recordValues).")";
if($mysqli->query($query))
$inserted++;
else
die("<h1>".$mysqli->error."</h1>");
}
echo $dbf->recordCount." records in file => ".$inserted." imported to MySQL<br/><br/>";
echo ($dbf->recordCount==$inserted)?'Success!<br/><a href="index.php">Start Over</a>':"Error.";
} else {
$result = $mysqli->query("SHOW TABLES");
$tables = $result->fetch_all();
?>
<form action="index.php" method="POST" enctype="multipart/form-data">
<br/>
dBase File:
<input type="file" name="dbfFile" id="dbfFile">
<br/><br/>
Destination Table:
<select name="destinationTable"><?php foreach($tables as $table) echo "<option>".$table[0]."</option>";?></select>
<br/><br/>
<input type="submit" value="Upload" name="submit" style="width: 200px;">
</form>
<?php
}
?>
</center>
</div>
</body>
</html>