PHP通过Ajax导出/下载MySQL备份 [英] PHP export/download MySQL backup via ajax

查看:93
本文介绍了PHP通过Ajax导出/下载MySQL备份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下PHP(MySQL备份/下载)脚本转储一个.sql备份文件,该文件已压缩并自动下载到客户端.

The following PHP (MySQL backup/download) script dumps a .sql backup file, which is gzipped and automatically downloaded to the client.

在单独的.php文件中运行此脚本会立即导致文件下载到客户端(该文件是有效的压缩的.sql导出/备份文件).到目前为止一切都很好.

Running this script in a separate .php file instantly results in a file downloading to the client (the file is a valid zipped .sql export/backup file). All good so far.

$database = 'mydbname';
$user = 'myusername';
$pass = 'mypass';
$host = 'localhost';
$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$mime = "application/x-gzip";
header( "Content-Type: " . $mime );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
passthru("mysqldump --user={$user} --password={$pass} --host={$host} {$database} | gzip --best");

请注意,直到我将以下行添加到我的.user.ini文件(共享主机帐户上的本地php.ini文件)后,以上内容才起作用:

Note that the above wasn't working until I added this line to my .user.ini (local php.ini on a shared hosting account) file:

output_buffering = "ON"

但是,将代码移动到ajax.php文件中并通过jQuery $ .ajax()从JavaScript click事件中调用它之后,它将二进制数据发送到ajax回调中,而不是直接发送到客户端.

However, after moving the code into an ajax.php file, and calling it from the JavaScript click event via jQuery $.ajax(), it sends binary data into ajax callback instead of sending directly to client.

$.ajax({
    url: "ajax.php",
    type: "post",
    data: "request=do_db_backup_now",
    success: function(recd){
        alert(recd);
        $('#btnMysqlBkup').hide();
    }
});

这时我需要怎么做才能将 recd 变量中的数据作为下载内容发送给客户端?

What would I need to do at this point to send the data in the recd variable as a download to the client?

推荐答案

要使此工作有效,我必须用以下代码替换 $.ajax() jQuery构造:

To make this work, I had to replace the $.ajax() jQuery construct with this:

$(document).on('click', '#btnMysqlBkup', function(){
    var now = new Date();
    var dtcode = now.toISOString().replace(/[-:]|T/g,"").slice(2,14);

    var oReq = new XMLHttpRequest();
    oReq.open("POST", "ajax_mysql_bkup.php", true);
    oReq.responseType = "blob";

    oReq.onload = function(oEvent) {
        var blob = oReq.response;
        var link=document.createElement('a');
        link.href=window.URL.createObjectURL(blob);
        link.download="MySQL_bkup_"+dtcode+".zip";
        link.click();
    };

    oReq.send();
});

然后,PHP代码开始工作,结果作为功能性zip文件自动下载到本地文件系统中.

Then, the PHP code worked, and the result automatically downloaded to the local file system as a functional zip file.

$database = 'mydbname';
$user = 'myusername';
$pass = 'mypass';
$host = 'localhost';

$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$mime = "application/x-gzip";

header( "Content-Type: " . $mime );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );

passthru("mysqldump --user={$user} --password={$pass} --host={$host} {$database} | gzip --best");

替代PHP-(还将压缩备份存储在服务器上,以及下载到客户端):

$database = 'mydbname';
$user = 'myusername';
$pass = 'mypass';
$host = 'localhost';

$filename = "backup-" . date("d-m-Y") . ".sql.gz";
$fqfn = "/home/myacctname/_sql_BKUPs/" . $filename;
$mime = "application/x-gzip";

exec("mysqldump --user={$DBUSER} --password={$DBPASSWD} --host={$HOST} {$DATABASE} | gzip -9 > {$fqfn}");
header( "Content-Type: " . $mime );
header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
passthru( "cat {$fqfn}" );

来源:

通过jQuery.Ajax下载文件< ===请对此答案进行投票

Sources:

Download a file by jQuery.Ajax <=== Please upvote this answer

使用jquery ajax下载二进制文件

如何格式化JavaScript日期

使用.php文件生成MySQL转储

如何修复标题已已发送"PHP中的错误

这篇关于PHP通过Ajax导出/下载MySQL备份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆