Mini Shell
<?php
session_start();
if(!isset($_SESSION['login'])) {
header('LOCATION:login.php'); die();
}
$conn = mysqli_connect("localhost", "duytan", "tandb", $_POST['db_name']);
if (file_exists('./'.$_POST['db_file']) && !empty($_POST['db_name'])) {
//move_uploaded_file($_FILES["backup_file"]["tmp_name"], $_FILES["backup_file"]["name"]);
$response = restoreMysqlDB('./'.$_POST['db_file'], $conn);
echo "<br><h2>DB name: ".$_POST['db_name']."</h2><a href='index.php'>Quay về Trang chủ</a>";
}
function restoreMysqlDB($filePath, $conn)
{
$sql = '';
$error = '';
if (file_exists($filePath)) {
$lines = file($filePath);
foreach ($lines as $line) {
// Ignoring comments from the SQL script
if (substr($line, 0, 2) == '--' || $line == '') {
continue;
}
$sql .= $line;
if (substr(trim($line), - 1, 1) == ';') {
$result = mysqli_query($conn, $sql);
if (! $result) {
$error .= mysqli_error($conn) . "\n";
}
$sql = '';
}
} // end foreach
if ($error) {
$response = array(
"type" => "error",
"message" => $error
);
} else {
$response = array(
"type" => "success",
"message" => "Database Restore Completed Successfully."
);
}
//exec('rm ' . $filePath);
} // end if file exists
return $response;
}
?>
<html>
<head>
<title>Restore DB</title>
<style>
body {
max-width: 550px;
font-family: "Segoe UI", Optima, Helvetica, Arial, sans-serif;
}
#frm-restore {
background: #aee5ef;
padding: 20px;
border-radius: 2px;
border: #a3d7e0 1px solid;
}
.form-row {
margin-bottom: 20px;
}
.input-file {
background: #FFF;
padding: 10px;
margin-top: 5px;
border-radius: 2px;
}
.btn-action {
background: #333;
border: 0;
padding: 10px 40px;
color: #FFF;
border-radius: 2px;
}
.response {
padding: 10px;
margin-bottom: 20px;
border-radius: 2px;
}
.error {
background: #fbd3d3;
border: #efc7c7 1px solid;
}
.success {
background: #cdf3e6;
border: #bee2d6 1px solid;
}
</style>
</head>
<body>
<h2>Import DB</h2>
<?php
if (! empty($response)) {
?>
<div class="response <?php echo $response["type"]; ?>">
<?php echo nl2br($response["message"]); ?>
</div>
<?php
}else{
?>
<form method="post" action="" id="frm-restore">
<div class="form-row">
<label>Chọn file mẫu:</label>
<select name="db_file" class="input-file">
<?php
foreach (glob("*.sql") as $filename) {
echo "<option value='".$filename."'>".$filename."</option>";
}?>
</select><br>
<label>DB name:</label>
<div>
<input type="text" name="db_name" class="input-file" value="<?=$_GET['db'];?>"/>
</div>
</div>
<div>
<input type="submit" name="restore" value="Import"
class="btn-action" />
</div>
</form>
<?php } ?>
</body>
</html>