信息系统项目管理师_2024年软考学习应考交流_信息系统项目管理师考试

 找回密码
 马上注册

QQ登录

只需一步,快速开始

查看: 706|回复: 0
打印 上一主题 下一主题

一键导出MySQL数据库并zip压缩的php代码

[复制链接]
  • TA的每日心情
    开心
    2016-1-24 10:15
  • 签到天数: 39 天

    [LV.5]常住居民I

    升级  83.33%

    跳转到指定楼层
    楼主
    发表于 2013-2-24 11:47:10 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    有时候,你的用户要求添加一个选项,导出整个数据库到一个SQL文件。虽然phpMyAdmin,以及Navicat有这个功能,但你的用户想要更简单点怎么办?
    以下是如何一键导出MySQL数据库的php代码。
    新建一个名为backup.php的文件,复制粘贴以下代码,然后编辑数据库连接设置和mysqldump的路径。有必要的话,你还可以添加一个backup.php超链接到你的程序里:
    <A href="backup.php">导出整个数据库</A>
    请注意,第一个php代码执行的时候,会导出zip压缩后的sql文件,所以此代码所在文件夹需要可写的权限。
    如果你没有写的权限,请使用第二个php代码,缺点是导出的sql文件不会被zip压缩。
    此代码需要可写权限:
    <?php
      
    $username = "root";  
    $password = "";  
    $hostname = "localhost";  
    $dbname   = "cars";
      
    // if mysqldump is on the system path you do not need to specify the full path
    // simply use "mysqldump --add-drop-table ..." in this case
    $dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";
    $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
        --user=$username ";
    if ($password)  
            $command.= "--password=". $password ." ";  
    $command.= $dbname;
    $command.= " > " . $dumpfname;
    system($command);
      
    // zip the dump file
    $zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";
    $zip = new ZipArchive();
    if($zip->open($zipfname,ZIPARCHIVE::CREATE))  
    {
       $zip->addFile($dumpfname,$dumpfname);
       $zip->close();
    }
      
    // read zip file and send it to standard output
    if (file_exists($zipfname)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($zipfname));
        flush();
        readfile($zipfname);
        exit;
    }
    ?>
    此代码不需要可写权限:
    <?php
    ob_start();
      
    $username = "root";  
    $password = "";  
    $hostname = "localhost";  
    $dbname   = "cars";
      
    // if mysqldump is on the system path you do not need to specify the full path
    // simply use "mysqldump --add-drop-table ..." in this case
    $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
        --user=$username ";
    if ($password)  
            $command.= "--password=". $password ." ";  
    $command.= $dbname;
    system($command);
      
    $dump = ob_get_contents();  
    ob_end_clean();
      
    // send dump file to the output
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($dbname . "_" .  
        date("Y-m-d_H-i-s").".sql"));
    flush();
    echo $dump;
    exit();]]>
    ?>

    原文:http://webcheatsheet.com/php/how_to_create_a_dump_of_mysql_database_in_one_click.php

    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏 转播转播 分享分享 顶 踩
    您需要登录后才可以回帖 登录 | 马上注册

    本版积分规则

    小黑屋|手机版|Archiver|信息系统项目管理师_软考交流平台. ( 鄂ICP备11002878号-1  公安备案号:42011102001150

    GMT+8, 2025-7-5 23:44

    Software by Discuz! X3.2

    © 2001-2013 SKIN BY DSVUE

    快速回复 返回顶部 返回列表