Very practical feature!
PHP code:
隐藏内容:登录后可查看
<?php
// 连接到 MySQL 数据库
$host = "localhost"; // MySQL 服务器地址
$username = "your_username"; // MySQL 用户名
$password = "your_password"; // MySQL 密码
$dbname = "your_database"; // MySQL 数据库名
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 上传文件
if (isset($_FILES["file"]) && $_FILES["file"]["error"] == 0) {
$target_dir = "/path/to/upload/directory/"; // 修改为你的上传目录
$target_file = $target_dir . basename($_FILES["file"]["name"]);
$fileType = pathinfo($target_file, PATHINFO_EXTENSION);
$allowedTypes = array('sql'); // 仅允许上传 SQL 文件
if (in_array($fileType, $allowedTypes)) {
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
// 清空数据库
$tables = $conn->query("SHOW TABLES")->fetch_all();
foreach ($tables as $table) {
$conn->query("DROP TABLE IF EXISTS {$table[0]}");
}
// 导入 SQL 文件
$sql = file_get_contents($target_file);
if ($conn->multi_query($sql)) {
echo "SQL 文件成功导入到数据库。";
} else {
echo "导入 SQL 文件时发生错误:" . $conn->error;
}
} else {
echo "上传文件失败。";
}
} else {
echo "只允许上传 SQL 文件。";
}
} else {
echo "请选择要上传的文件。";
}
// 关闭数据库连接
$conn->close();
?>
Please place $host,$username,$password,$dbname,$target_dir Replace with your actual information. The uploaded SQL file must be stored in $target_dir The directory is used exclusively for uploading SQL files; if no errors occur during the upload and import process, the database will be cleared and the SQL file will be imported.
Note: In production environments, for security reasons, uploaded files should undergo additional validation and filtering – for example, by restricting the allowed file types and sizes to prevent file upload vulnerabilities.
HTMLform:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Upload SQL File</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-g5AX/vyqo5KOGxz5AAn/AY1dRItbsdw7tHtzNjh9avsgzP0nckdKP/Jc1wJfONjK" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Upload SQL File</h1>
<form action="upload_sql.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sqlFile">Select SQL File</label>
<input type="file" class="form-control-file" id="sqlFile" name="sqlFile">
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@2.9.3/dist/umd/popper.min.js" integrity="sha384-lb6U1+A6OHXWnogQIgUpkzwFJa5yk5PbRyfPYpV1FaaLnNm/KTMc2QH8Heq7ZjKd" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" integrity="sha384-LpW1aXBBv/smfihSkStFCTyJf35QLlAdAGl7Yh2i2IDJnVWj9X3v7FJITgG14x7V" crossorigin="anonymous"></script>
</body>
</html>
Please note that this form contains only a single file input field and a submit button; you can use it to upload an SQL file and submit it to a destination named upload_sql.php Processing script: You need to modify the form element names and the target of the submission operation according to your actual requirements.
When the two are combined – for example:
隐藏内容:会员可查看
<?php
if($_FILES['sqlFile']){
// 连接到 MySQL 数据库
$host = "localhost"; // MySQL 服务器地址
$username = "test_4s5_cn"; // MySQL 用户名
$password = "wnxxtJTa3y4SPPrr"; // MySQL 密码
$dbname = "test_4s5_cn"; // MySQL 数据库名
$conn = new mysqli($host, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
// 上传文件
if (isset($_FILES["sqlFile"]) && $_FILES["sqlFile"]["error"] == 0) {
$target_dir = __DIR__."/path/"; // 修改为你的上传目录
// 创建指定目录
if (!mkdir($target_dir, 0777, true)) {
throw new Exception('Failed to create directory.');
}
$target_file = $target_dir . basename($_FILES["sqlFile"]["name"]);
$fileType = pathinfo($target_file, PATHINFO_EXTENSION);
$allowedTypes = array('sql'); // 仅允许上传 SQL 文件
if (in_array($fileType, $allowedTypes)) {
if (move_uploaded_file($_FILES["sqlFile"]["tmp_name"], $target_file)) {
// 清空数据库
$tables = $conn->query("SHOW TABLES")->fetch_all();
foreach ($tables as $table) {
$conn->query("DROP TABLE IF EXISTS {$table[0]}");
}
// 导入 SQL 文件
$sql = file_get_contents($target_file);
if ($conn->multi_query($sql)) {
echo "SQL 文件成功导入到数据库。";
} else {
echo "导入 SQL 文件时发生错误:" . $conn->error;
}
} else {
echo "上传文件失败。";
}
} else {
echo "只允许上传 SQL 文件。";
}
} else {
echo "请选择要上传的文件。";
}
if (is_dir($target_dir)) { // 如果目录存在
$files = glob($target_dir . '/*'); // 获取目录下的所有文件和子目录
foreach ($files as $file) {
if (is_file($file)) { // 如果是文件则直接删除
unlink($file);
} else { // 如果是子目录则递归调用自身
deleteDir($file);
}
}
rmdir($target_dir); // 删除目录
}
// 关闭数据库连接
$conn->close();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Upload SQL File</title>
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" integrity="sha384-g5AX/vyqo5KOGxz5AAn/AY1dRItbsdw7tHtzNjh9avsgzP0nckdKP/Jc1wJfONjK" crossorigin="anonymous">
</head>
<body>
<div class="container">
<h1>Upload SQL File</h1>
<form action="<?=$SERVER['SCRIPT_NAME']?>" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="sqlFile">Select SQL File</label>
<input type="file" class="form-control-file" id="sqlFile" name="sqlFile">
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
</div>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.6.0.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@2.9.3/dist/umd/popper.min.js" integrity="sha384-lb6U1+A6OHXWnogQIgUpkzwFJa5yk5PbRyfPYpV1FaaLnNm/KTMc2QH8Heq7ZjKd" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" integrity="sha384-LpW1aXBBv/smfihSkStFCTyJf35QLlAdAGl7Yh2i2IDJnVWj9X3v7FJITgG14x7V" crossorigin="anonymous"></script>
</body>
</html>