Tinkphp5,程序员深度学习

本文介绍了如何使用PHP进行数据库备份与还原操作。首先,通过编写函数实现数据库的导出,包括创建备份文件、写入数据库信息和数据。接着,提供了一个用于导入.sql文件到数据库的函数。此外,还创建了一张用于保存备份内容的数据库表,并在后台控制器中调用备份函数,将备份信息写入数据库。最后讨论了前端如何配合jszip插件下载备份文件,以解决PHP版本不支持zip压缩的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public static function export($dir=‘data’) {

//保存路径,默认为站点根目录下的data文件夹里

path=ROOTPATH.path = ROOT_PATH .path=ROOTPATH.dir.DIRECTORY_SEPARATOR;

//获取数据库名

$dbname=config(“database.database”);

//检查目录是否存在

if(!is_dir($path)) {

//新建目录

mkdir($path, 0777, true);

}

//检查目录是否可写

if(!is_writable($path)) {

chmod($path,0777);

}

//检查文件是否存在

fileName=fileName=fileName=dbname.’_’.date(“Y-m-d”,time()).’.sql’;

//数据库保存相对路径

$filePath = dir.DIRECTORYSEPARATOR.dir.DIRECTORY_SEPARATOR.dir.DIRECTORYSEPARATOR.fileName;

//硬盘保存绝对路径

$savepath = path.path.path.fileName;

try{

if(!file_exists($savepath)) {

//获取mysql版本

$version = Db::query(“select version() as ver”);

$ver = $version[0][‘ver’];

$info = “-- ----------------------------\r\n”;

$info .= “-- 日期:”.date(“Y-m-d H:i:s”,time())."\r\n";

info.="−−MySQL−".info .= "-- MySQL - ".info.="MySQL".ver." : Database - “.$dbname.”\r\n";

$info .= “-- ----------------------------\r\n\r\n”;

info.="CREATEDATAbaseIFNOTEXISTS‘".info .= "CREATE DATAbase IF NOT EXISTS `".info.="CREATEDATAbaseIFNOTEXISTS".dbname."` DEFAULT CHARACTER SET utf8 ;\r\n\r\n";

info.="USE‘".info .= "USE `".info.="USE".dbname."`;\r\n\r\n";

file_put_contents(savepath,savepath,savepath,info,FILE_APPEND);

//查询所有表

$sql=“show tables”;

//执行原生SQL语句

result=Db::query(result=Db::query(result=Db::query(sql);

foreach ($result as k=>k=>k=>v) {

//查询表结构

$table = KaTeX parse error: Expected group after '_' at position 13: v['Tables_in_̲'.dbname];

sqltable="showcreatetable".sql_table = "show create table ".sqltable="showcreatetable".table;

res=Db::query(res = Db::query(res=Db::query(sql_table);

$info_table = “-- ----------------------------\r\n”;

infotable.="−−Tablestructurefor‘".info_table .= "-- Table structure for `".infotable.="Tablestructurefor".table."`\r\n";

$info_table .= “-- ----------------------------\r\n\r\n”;

infotable.="DROPTABLEIFEXISTS‘".info_table .= "DROP TABLE IF EXISTS `".infotable.="DROPTABLEIFEXISTS".table."`;\r\n\r\n";

$info_table .= $res[0][‘Create Table’].";\r\n\r\n";

//查询表数据

$info_table .= “-- ----------------------------\r\n”;

infotable.="−−Dataforthetable‘".info_table .= "-- Data for the table `".infotable.="Dataforthetable".table."`\r\n";

$info_table .= “-- ----------------------------\r\n\r\n”;

file_put_contents(savepath,savepath,savepath,info_table,FILE_APPEND);

sqldata="select∗from".sql_data = "select * from ".sqldata="selectfrom".table;

data=Db::query(data = Db::query(data=Db::query(sql_data);

count=count(count= count(count=count(data);

if($count<1) continue;

foreach ($data as $key => $value) {

KaTeX parse error: Expected '}', got 'EOF' at end of input: …"INSERT INTO `{table}` VALUES (";

foreach($value as $column) {

//对单引号和换行符进行一下转义

KaTeX parse error: Can't use function '\r' in math mode at position 34: …ce( array("'","\̲r̲\n"), array("\'…column);

//当值为空时,使用default默认

sqlStr.=empty(sqlStr .=empty(sqlStr.=empty(column) ? “default, " : “’”.$column.”’, ";

}

//去掉最后一个逗号和空格

sqlStr=substr(sqlStr = substr(sqlStr=substr(sqlStr,0,strlen($sqlStr)-2);

$sqlStr .= “);\r\n”;

//KaTeX parse error: Expected '}', got 'EOF' at end of input: …"INSERT INTO `{table}` VALUES (’" . str_replace(array("\r", “\n”), array(’\r’, ‘\n’), implode("’, ‘", $value)) . "’);\n";

file_put_contents(savepath,savepath,savepath,sqlStr,FILE_APPEND);

}

$info = “\r\n”;

file_put_contents(savepath,savepath,savepath,info,FILE_APPEND);

}

//计算文件大小

size=filesize(size=filesize(size=filesize(savepath);

return array(‘code’ => 1, ‘msg’ => ‘数据库备份成功’,‘name’=>fileName,′size′=>fileName,'size'=>fileName,size=>size,‘path’=>$filePath);

}else{

return array(‘code’ => 0, ‘msg’ => ‘备份文件已存在’);

}

} catch (\Exception $e) {

return array(‘code’ => 0, ‘msg’ => ‘备份数据库失败,Error:’.$e);

}

}

/**

  • 将sql文件导入到数据库

  • @param string $sqlfile sql文件

*/

public static function import($sqlfile) {

if(!file_exists($sqlfile)) {

return array(‘code’ => 0, ‘msg’ => ‘备份文件不存在’);

}

try {

// 创建保存sqlsql语句的数组

$sqls = array ();

file=fopen(file = fopen (file=fopen(sqlfile, “rb” );

// 创建表缓冲变量

$table_buffer = ‘’;

while ( ! feof ($file) ) {

// 读取每一行sql

$row = fgets ( $file);

// 如果结尾没有包含’;’(即为一个完整的sql语句,这里是插入语句),并且不包含’ENGINE=’(即创建表的最后一句)

if (! preg_match ( ‘/;/’, $row ) || preg_match ( ‘/ENGINE=/’, $row )) {

// 将本次sql语句与创建表sql连接存起来

$table_buffer .= $row;

// 如果包含了创建表的最后一句

if (preg_match ( ‘/ENGINE=/’, $table_buffer)) {

//执行sql语句创建表

Db::execute($table_buffer);

// 清空当前,准备下一个表的创建

$table_buffer = ‘’;

}

// 跳过本次

continue;

}

//执行sql语句

Db::execute($row);

}

fclose ($file);

return array(‘code’ => 1, ‘msg’ => ‘还原数据库成功’);

}

catch (\Exception $e) {

return array(‘code’ => 0, ‘msg’ => ‘还原数据库失败,Error:’.$e);

}

}

}

二、创建一张数据库表,用于保存备份内容


– Table structure for database


DROP TABLE IF EXISTS database;

CREATE TABLE database (

id int(11) NOT NULL AUTO_INCREMENT,

name varchar(100) DEFAULT NULL,

size varchar(20) DEFAULT ‘0’,

path varchar(255) DEFAULT NULL,

create_time int(11) DEFAULT NULL,

update_time int(11) DEFAULT NULL,

delete_time int(11) DEFAULT NULL,

PRIMARY KEY (id)

) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

三、备份数据库

thinkphp后端控制器代码

<?php namespace app\admin\controller; use think\Controller; use think\Db; use app\common\Backup; /** ** @name='备份' */ public function backup() { if(request()->isPost()){ $res=Backup::export(); if($res['code']==1){ //写入数据库 DatabaseModel::create([ 'name' => $res['name'], 'size' => $res['size'], 'path' => $res['path'] ]); $this->success($res['msg']); }else{ $this->error($res['msg']); } } } ### 四、下载备份文件 1、下载数据库备份文件,需要先将.sql文件压缩成zip,因此建议前端配合使用jszip插件,jszip教程参数这篇文章:[https://blog.csdn.net/qq15577969/article/details/115549729]( ) > **Ps**:有的朋友可能会说,直接使用php自带的zip压缩功能不就可以了吗,何必那么麻烦。这个当然也可以,但前提是你使用的php版本安装了php\_zip.dll扩展,然后才能使用ZipArchive类实现zip压缩功能。博主使用的php5.6版本是没有这个扩展的,而且博主开发的系统是要授权给用户使用的,用户不一定都懂安装php扩展,所以为了保险起见,直接选择了配合前端jszip插件来实现压缩的方案。 2、前端js代码:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值