前端代码
<form method="post" action="recovery_mysqldb.php" enctype="multipart/form-data">
<table width="100%" border="0">
<tbody>
<tr>
<td width="25%" height="50" align="right" class="backup-text">数据库还原:</td>
<td colspan="3"></td>
</tr>
<tr>
<td width="25%" height="50" align="right">请选择路径:</td>
<td colspan="3"><input type="file" name="myfile" id="myfile" style="width: 100%;" placeholder=""></td>
</tr>
<tr>
<td height="50"> </td>
<td colspan="3"><span class="text-prompt">*(谨慎使用)选择 name.sql 文件路径点击还原按钮即可还原数据表及数据,还原后的数据是选择sql文件里头的数据。</span></td>
</tr>
<tr>
<td height="50" align="right" valign="middle"> </td>
<td height="50" align="center" valign="middle">
</td>
<td align="center" valign="middle">
<button type="submit" name="submit" class="btn btn-success" onclick="return stop()">还原数据库</button>
<button type="reset" class="btn btn-default"> 重新设置 </button>
</td>
<td width="25%" align="left" valign="middle"> </td>
</tr>
</tbody>
</table>
</form>
发送请求后台代码
<!--
作者:87046766@qq.com
时间:2018-01-16
描述:版权所有违者必究
-->
<meta charset="utf-8">
<?php
ob_start();
session_start();
if($_SESSION['admin']!=''){
//$upload_dir="./upload files";
$upload_file=iconv("UTF-8","Big5",$_FILES["myfile"]["name"]);
if(move_uploaded_file($_FILES["myfile"]["tmp_name"],$upload_file)){
//连接数据库
$conn=mysql_connect("localhost","root","root");
mysql_select_db("evaluation_system",$conn);
mysql_query("set names utf8");
//还原数据库要执行的语句
$dbname="evaluation_system";
$sql="";
//查询所有数据表名
$sql_tablist="show tables";
$rs_tablist=mysql_query($sql_tablist);
while($tablist=mysql_fetch_assoc($rs_tablist)){
$sql.="drop table `".$tablist['Tables_in_'.$dbname]."`;";
}
//读取上传到的文件
//打开文件
$handle=fopen($_FILES["myfile"]["name"],"r");
//读取文件
$sql.=fread($handle,filesize($_FILES["myfile"]["name"]));
//关闭文件
fclose($handle);
//不能批量执行sql语句分割以;sql
$array=explode(";",$sql);
//循环执行分隔开的sql语句
foreach($array as $value){
mysql_query($value);
}
//echo $sql;
//删除服务器端文件
unlink($_FILES["myfile"]["name"]);
echo "<script type='text/javascript'>alert('数据库还原成功!');window.location.href='admin_mysqldb.php';</script>";
}else{
echo "<script type='text/javascript'>alert('出错啦!请重新选择还原文件!');window.location.href='admin_mysqldb.php';</script>";
}
}
else echo "<script language='javascript'>top.location.href='index.php'</script>";
?>
网友评论