The following PHP (MySQL backup/download) script dumps a .sql backup file, which is gzipped and automatically downloaded to the client.
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");
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"
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();
}
});
What would I need to do at this point to send the data in the recd
variable as a download to the client?