Posted in PHP onApril 05, 2020
本文实例讲述了TP5框架实现的数据库备份功能。分享给大家供大家参考,具体如下:
1、效果图
2、下载扩展类( \extands\org\Baksql.php)
3、在 \public\static 里新建一个data 文件夹用来存放 .sql 的文件
4、使用方法
controller
<?php namespace app\index\controller; use think\Controller; class Backup extends Controller { //数据库备份 public function bak(){ $type=input("tp"); $name=input("name"); $sql=new \org\Baksql(\think\Config::get("database")); switch ($type) { case "backup": //备份 $info = $sql->backup(); $this->success("$info",'index/backup/bak'); break; case "dowonload": //下载 $info = $sql->downloadFile($name); $this->success("$info",'index/backup/bak'); break; case "restore": //还原 $info = $sql->restore($name); $this->success("$info",'index/backup/bak'); break; case "del": //删除 $info = $sql->delfilename($name); $this->success("$info",'index/backup/bak'); break; default: //获取备份文件列表 return $this->fetch("bak",["list"=>$sql->get_filelist()]); } } }
view
<!--我用的是 H+ 的模板,样式可以自己调--> <body class="gray-bg"> <div class="wrapper wrapper-content animated fadeInRight"> <div class="row"> <div class="col-sm-12"> <div class="ibox float-e-margins"> <div class="ibox-title"> <h5>数据库备份</h5> <div class="ibox-tools"> <a class="collapse-link"> <i class="fa fa-chevron-up"></i> </a> </div> </div> <div class="ibox-content"> <div class=""> <a class="btn btn-primary " href="{:url('bak',['tp'=>'backup'])}" rel="external nofollow" οnclick="return confirm('备份数据的时间较长,确定要备份所有数据吗?')"><i class="glyphicon glyphicon-plus"></i> <span class="bold">添加备份</span></a> </div> <table class="table table-striped table-bordered table-hover dataTables-example" style="text-align: center"> <thead> <tr> <td>序号</td> <td>备份名称</td> <td>备份时间</td> <td>备份大小</td> <td>操作</td> </tr> </thead> <tbody> {volist name="list" id="vo"} <tr class="gradeX" > <td>{$key+1}</td> <td>{$vo.name}</td> <td>{$vo.time}</td> <td>{$vo.size}</td> <td width="25%"> <a href="{:url('bak',['tp'=>'dowonload','name'=>$vo.name])}" rel="external nofollow" class="btn btn-success "><i class="glyphicon glyphicon-download-alt"></i> <span class="bold">下载</span></a> <a href="{:url('bak',['tp'=>'restore','name'=>$vo.name])}" rel="external nofollow" class="btn btn-info " οnclick="return confirm('备份还原后仅会显示当前备份的数据库的信息,您确定还原备份吗 ?')"><i class="glyphicon glyphicon-repeat"></i> <span class="bold">还原</span></a> <a href="{:url('bak',['tp'=>'del','name'=>$vo.name])}" rel="external nofollow" class="btn btn-warning" οnclick="return confirm('数据库备份一旦删除不可找回,您确定操作吗?')"><i class="fa fa-warning"></i> <span class="bold">删除</span></a> </td> </tr> {/volist} </tbody> </table> </div> </div> </div> </div> </div> </body>
5、Refresh the web page!!!over,over,over
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
TP5框架实现的数据库备份功能示例
- Author -
Anwug!声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@