使用PHP强制多个文件下载

时间:2012-08-07 10:04:26

标签: php mysql download

我正在尝试编写一个小脚本,以便在单独的文件中同时备份我的所有localhost数据库。目前我的脚本执行备份部分,但在一个文件中进行下载。 (所以我得到像30MB information_schema_backup.sql这样的东西)我想要的是每个数据库的db_name_backup.sql。这是我的代码。

<?

    function backup_dbs()
    {
        global $db_name;
        global $dosyaadi; //dosyaadi means filename.
        $return='';

        /*
            Backup Part Here;
            I can share if anyone needs.
            I will post this on my blog anyway.
        */

        //save file
        $dosyaadi=$db_name.'-backup-'.time().'.sql';
        header("Content-type: application/octet-stream");
        header("Content-disposition: attachment;filename=$dosyaadi");
        echo $return;
    }

        $db_host = 'localhost';
        $db_user = 'root';
        $db_pass = '';

        $db = mysql_connect($db_host, $db_user, $db_pass);

        mysql_query("SET NAMES 'utf8'", $db);
        mysql_query("SET CHARACTER SET utf8", $db); 
        mysql_query("SET COLLATION_CONNECTION = 'utf8_general_ci'", $db); 

        $res = mysql_query("SHOW DATABASES");

        while ($row = mysql_fetch_assoc($res)) {
            $db_name =  $row['Database'];
            mysql_select_db($db_name, $db);
            backup_dbs();
        }
    die();
?>

提前致谢。

0 个答案:

没有答案