dd

Thinkphp数据库在线备份下载和还原

汉王 PHP+MySQL 收藏
我们在首页控制器Index重定向到了Bak控制器中,即数据库管理所有操作方法。备份的数据库放在根目录文件夹databak中,接着引入MySQLReback类。
$DataDir = "databak/"; 
mkdir($DataDir); 
import("Common.Org.MySQLReback"); 
$mr = new MySQLReback($config); 
$mr->setDBName(C('DB_NAME'));
备份:
if ($_GET['Action'] == 'backup') { 
    $mr->backup(); 
    echo "<script>document.location.href='" . U("Bak/index") . "'</script>"; 
   $this->success( '数据库备份成功!'); 
}
还原:
$mr->recover($_GET['File']); 
echo "<script>document.location.href='" . U("Bak/index") . "'</script>";
删除:
if ($_GET['Action'] == 'Del') { 
    if (@unlink($DataDir . $_GET['File'])) { 
        // $this->success('删除成功!'); 
        echo "<script>document.location.href='" . U("Bak/index") . "'</script>"; 
    } else { 
        $this->error('删除失败!'); 
    } 
}
读取备份的所有数据库列表
$lists = $this->MyScandir('databak/');
下载:
if ($_GET['Action'] == 'download') { 
    function DownloadFile($fileName) { 
        ob_end_clean(); 
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
        header('Content-Description: File Transfer'); 
        header('Content-Type: application/octet-stream'); 
        header('Content-Length: ' . filesize($fileName)); 
        header('Content-Disposition: attachment; filename=' . basename($fileName)); 
        readfile($fileName); 
        } 
        DownloadFile($DataDir . $_GET['file']); 
        exit(); 
    } 
}

下载地址

dd