-
Notifications
You must be signed in to change notification settings - Fork 0
/
import.php
64 lines (53 loc) · 2.15 KB
/
import.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
<?php
require_once 'settings.php';
header("Content-Type: text/html; charset=UTF-8");
if (!file_exists(DUMP_DIR)) {
die('<p>Dump folder does not exist. Please check the folder path.</p>');
}
$conn = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);
if ($conn->connect_error) {
die('<p>Connection failed: ' . htmlspecialchars($conn->connect_error) . '</p>');
}
// Retrieve the database character set
$charsetRes = $conn->query("SELECT @@character_set_database AS charset");
if ($charsetRes === false) {
die('<p>Error fetching character set: ' . htmlspecialchars($conn->error) . '</p>');
}
$charset = $charsetRes->fetch_assoc()['charset'];
$conn->set_charset($charset);
$files = array_diff(scandir(DUMP_DIR), array('..', '.'));
$totalFiles = count($files);
$currentIndex = 1;
foreach ($files as $file) {
if (pathinfo($file, PATHINFO_EXTENSION) == 'sql') {
$filePath = DUMP_DIR . '/' . $file;
echo "<p><b>Processing $file [$currentIndex/$totalFiles]...</b></p>";
flush();
$commands = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($commands === false) {
echo "<p>Error reading file $file</p>";
continue;
}
$commandCount = count($commands);
$currentCommandIndex = 1;
foreach ($commands as $command) {
if (!empty(trim($command))) { // Avoid empty lines
if (!$conn->query($command)) {
echo "<p>Error executing command at line $currentCommandIndex in $file: " . htmlspecialchars($conn->error) . "</p>";
flush();
break; // Stop executing further commands after the first error
}
if ($currentCommandIndex % 100 == 0 || $currentCommandIndex === $commandCount) { // Progress update or final command
echo ". ";
flush();
}
$currentCommandIndex++;
}
}
echo "<p>Completed processing $file. Total commands: $commandCount.</p>";
flush();
$currentIndex++;
}
}
$conn->close();
echo "<p><b>All files uploaded successfully.</b></p>";