Codeigniter数据库备份不返回可读文件

时间:2014-07-02 08:58:03

标签: database codeigniter backup codeigniter-2

这是我的控制器功能,其中我使用数据库util库来为数据库创建备份。当我下载备份时,无法使用zip提取器打开zip文件。我该怎么办?

function backup_database() {

        $file_name = 'accounts';
        $date = date('@Y.m.d-H.ia');

        $name = $file_name . $date;

        // Load the DB utility class
        $this->load->dbutil();

        // Backup entire database and assign it to a variable
        $backup = & $this->dbutil->backup(array('filename' => "$name.sql"));

        // Load the file helper and write the file to server
        $this->load->helper('file');
        write_file("$name.zip", $backup);

        // Load the download helper and send the file to desktop
        $this->load->helper('download');
        force_download("$name.zip", $backup);
    }

1 个答案:

答案 0 :(得分:1)

这是我用来执行数据库备份的功能。

function _backup_db()
{
    $this->load->dbutil();
    $this->load->helper(array('file', 'download')); 

    $backup =& $this->dbutil->backup();

    $filename = 'backup-' . time() . ' .zip';

    write_file('/backups/' . $filename, $backup);
    force_download($filename, $backup); 
}

我唯一注意到的是备份()函数中的文件名。根据用户指南,您只需要将文件名添加到backup()数组中,如果它是.zip文件。

http://ellislab.com/codeigniter/user-guide/database/utilities.html#backup