//读取数据库备份文件
public function index(){
p
a
t
h
=
R
U
N
T
I
M
E
P
A
T
H
.
′
m
y
s
q
l
′
;
i
f
(
!
i
s
d
i
r
(
path = RUNTIME_PATH.'mysql'; if(!is_dir(
path=RUNTIMEPATH.′mysql′;if(!isdir(path)){
mkdir($path);
}
//scandir方法
$arr = array();
$data = scandir($path);
foreach ($data as $value){
if($value != '.' && $value != '..'){
$arr[] = $value;
}
}
rsort($arr);
// dump($arr);die;
$this->assign('list',$arr);
return $this->fetch();
// dump($arr);die;
// return $arr;
}
public function sc(){
$get = input('param.name');
$data = unlink(RUNTIME_PATH.'mysql/'.$get);
if($get){
$this->AjaxReturn(200,'成功');
}else{
$this->AjaxReturn(500,'失败');
}
}
public function download()
{
// $famlePath = $_GET['resum'];
// $file_dir = ROOT_PATH . 'public' . DS . 'uploads' . '/' . "$famlePath"; // 下载文件存放目录
$get = input('param.name');
$file_dir = RUNTIME_PATH.'mysql/'.$get;
// 检查文件是否存在
if (! file_exists($file_dir) ) {
$this->error('文件未找到');
}else{
// 打开文件
$file1 = fopen($file_dir, "r");
// 输入文件标签
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Accept-Length:".filesize($file_dir));
Header("Content-Disposition: attachment;filename=" . $file_dir);
ob_clean(); // 重点!!!
flush(); // 重点!!!!可以清除文件中多余的路径名以及解决乱码的问题:
//输出文件内容
//读取文件内容并直接输出到浏览器
echo fread($file1, filesize($file_dir));
fclose($file1);
exit();
}
}
/**
* 导出数据库备份
* baikeliang
* 2017-9-27
*/
public function exportDatabase(){
header(“Content-type:text/html;charset=utf-8”);
$path = RUNTIME_PATH.‘mysql/’;
$database = config(‘database’)[‘database’];
//echo “运行中,请耐心等待…
”;
$info = “-- ----------------------------\r\n”;
$info .= “-- 日期:”.date(“Y-m-d H:i:s”,time())."\r\n";
i
n
f
o
.
=
"
−
−
M
y
S
Q
L
−
5.5.52
−
M
a
r
i
a
D
B
:
D
a
t
a
b
a
s
e
−
"
.
info .= "-- MySQL - 5.5.52-MariaDB : Database - ".
info.="−−MySQL−5.5.52−MariaDB:Database−".database."\r\n";
$info .= “-- ----------------------------\r\n\r\n”;
i
n
f
o
.
=
"
C
R
E
A
T
E
D
A
T
A
b
a
s
e
I
F
N
O
T
E
X
I
S
T
S
‘
"
.
info .= "CREATE DATAbase IF NOT EXISTS `".
info.="CREATEDATAbaseIFNOTEXISTS‘".database."DEFAULT CHARACTER SET utf8 ;\r\n\r\n"; $info .= "USE
".$database."`;\r\n\r\n";
// 检查目录是否存在
if(is_dir($path)){
// 检查目录是否可写
if(is_writable($path)){
//echo '目录可写';exit;
}else{
//echo '目录不可写';exit;
chmod($path,0777);
}
}else{
//echo '目录不存在';exit;
// 新建目录
mkdir($path, 0777, true);
//chmod($path,0777);
}
// 检查文件是否存在
$file_name = $path.$database.'-'.date("Y-m-d",time()).'.sql';
if(file_exists($file_name)){
echo "<script>alert('数据备份文件已存在!');history.go(-1)</script>";
exit;
}
file_put_contents($file_name,$info,FILE_APPEND);
//查询数据库的所有表
$result = Db::query('show tables');
//print_r($result);exit;
foreach ($result as $k=>$v) {
//查询表结构
$val = $v['Tables_in_'.$database];
$sql_table = "show create table ".$val;
$res = Db::query($sql_table);
//print_r($res);exit;
$info_table = "-- ----------------------------\r\n";
$info_table .= "-- Table structure for `".$val."`\r\n";
$info_table .= "-- ----------------------------\r\n\r\n";
$info_table .= "DROP TABLE IF EXISTS `".$val."`;\r\n\r\n";
$info_table .= $res[0]['Create Table'].";\r\n\r\n";
//查询表数据
$info_table .= "-- ----------------------------\r\n";
$info_table .= "-- Data for the table `".$val."`\r\n";
$info_table .= "-- ----------------------------\r\n\r\n";
file_put_contents($file_name,$info_table,FILE_APPEND);
$sql_data = "select * from ".$val;
$data = Db::query($sql_data);
//print_r($data);exit;
$count= count($data);
//print_r($count);exit;
if($count<1) continue;
foreach ($data as $key => $value){
$sqlStr = "INSERT INTO `".$val."` VALUES (";
foreach($value as $v_d){
$v_d = str_replace("'","\'",$v_d);
$sqlStr .= "'".$v_d."', ";
}
//需要特别注意对数据的单引号进行转义处理
//去掉最后一个逗号和空格
$sqlStr = substr($sqlStr,0,strlen($sqlStr)-2);
$sqlStr .= ");\r\n";
file_put_contents($file_name,$sqlStr,FILE_APPEND);
}
$info = "\r\n";
file_put_contents($file_name,$info,FILE_APPEND);
}
echo "<script>alert('数据备份完成!');history.go(-1)</script>";
}