You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将ifsnop/mysqldump-php生成的MySQL数据库备份文件保存到本地而非服务器

解决ifsnop/mysqldump-php备份文件直接下载到本地的问题

Hey there! Let's get your backup to trigger a browser "Save As" window instead of storing it on the server. Your original code had two main issues: a syntax error in the database connection string, and you weren't capturing or outputting the backup content correctly to the browser.

方案一:直接输出到浏览器(推荐)

This approach skips saving the file to the server entirely and sends the backup content straight to the browser with the right headers to trigger a download.

<?php
include_once('../myfolder/mysqldump-php-master/src/Ifsnop/Mysqldump/Mysqldump.php');

try {
    // 发送下载头(必须在任何输出之前执行)
    header('Content-Type: application/sql');
    header('Content-Disposition: attachment; filename="mybackup.sql"');
    header('Content-Transfer-Encoding: Binary');

    // 修正连接字符串的语法错误!
    $dump = new Ifsnop\Mysqldump\Mysqldump(
        'mysql:host=myhost;dbname=myname', 
        'myuser', 
        'mypassword'
    );

    // 直接将备份内容输出到php://output(即浏览器)
    $dump->start('php://output');
} catch (Exception $e) {
    die('备份过程出错:' . $e->getMessage());
}
?>

方案二:捕获备份内容后输出

If you need to manipulate the backup content before sending it (like adding a timestamp to the filename or compressing it), you can use output buffering to capture the content first:

<?php
include_once('../myfolder/mysqldump-php-master/src/Ifsnop/Mysqldump/Mysqldump.php');

try {
    ob_start(); // 开启输出缓冲

    $dump = new Ifsnop\Mysqldump\Mysqldump(
        'mysql:host=myhost;dbname=myname', 
        'myuser', 
        'mypassword'
    );

    $dump->start('php://output'); // 将备份内容写入缓冲
    $backupContent = ob_get_clean(); // 获取缓冲内容并关闭缓冲

    // 发送下载头
    header('Content-Type: application/sql');
    // 可选:添加时间戳到文件名,比如 mybackup_20240520.sql
    $filename = 'mybackup_' . date('Ymd') . '.sql';
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Length: ' . strlen($backupContent)); // 让浏览器知道文件大小
    header('Content-Transfer-Encoding: Binary');

    echo $backupContent;
} catch (Exception $e) {
    die('备份过程出错:' . $e->getMessage());
}
?>

关键注意事项

  • 修正连接字符串: Your original code had a syntax error here – the database DSN needs to be a single string, not split with a semicolon outside quotes.
  • 无前置输出: Make sure there are no spaces, newlines, or other output before the header() calls (even in included files). Any output will break the headers and prevent the download from working.
  • 异常处理: Adding try-catch helps you debug issues like failed database connections or insufficient permissions to read tables/BLOBs.
  • 内容类型: Using application/sql instead of application/octet-stream helps browsers recognize the file type correctly.

内容的提问来源于stack exchange,提问作者Fabio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 15:07:38