canyin-project/ybcy/controllers/admin/VersionfileController.php

74 lines
2.4 KiB
PHP
Raw Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\controllers\admin;
use Yii;
use yii\web\Controller;
use Mysqldump;
use Zipdump;
class VersionfileController extends CommonController{
//在线数据库备份
public function actionMysqldump(){
//1.extend/Mysqldump.php
// 在config/web.php 注册服务Yii::$classMap['Mysqldump'] = '@app/extend/Mysqldump.php';
//2.控制器use Mysqldump; 引用extend/Mysqldump.php
Mysqldump::back();
echo json_encode(
array(
'code'=>200,
'msg'=>'数据库备份成功'
)
);
}
//备份整个项目目录
public function actionZipdumpdir(){
if(!file_exists('oldversion')){
mkdir('oldversion');
chmod('oldversion',0777,true);
}
$from =Yii::$app->basePath;
//dd($from);die;
$to = 'oldversion/zip'.date('Y-m-d H:i:s',time()).'.zip';
if(!file_exists($to)){
touch($to);
chmod($to,0777,true);
}
$zip_result = Zipdump::createZip($from, $to);
if($zip_result['success']=1&&$zip_result['message']==''){
echo json_encode(
['code'=>200,
'msg'=>'备份完成'
]
);
}else{
echo json_encode(
['code'=>500,
'msg'=>$zip_result['message']
]
);
}
}
//下载压缩包文件
public function actionUnziptodir(){
$request = Yii::$app->request;
if ($request->isGet){
$destination=Yii::$app->request->get()['downname'];
//下载前判断文件是否打包
if(!file_exists($destination)){
echo json_encode(['code'=>500,'msg'=>'压缩包不存在']);die;
}
//下载zip
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($destination)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($destination)); //告诉浏览器,文件大小
readfile($destination);
exit();
}else{
return false;
}
}
}