在CakePHP中设置数据库备份的下载文件路径

时间:2015-04-28 09:35:24

标签: cakephp

我想在下面给出的函数中以SQL格式设置我的数据库备份文件的下载文件路径。每当我尝试此功能时,文件都会下载到我本地计算机上的下载文件夹中,但我想将其下载到服务器上的指定目录中:

function admin_database_mysql_dump($tables = '*') {
ini_set('memory_limit', '1024M');
set_time_limit(0);

$return = '';

$modelName = $this->modelClass;

$dataSource = $this->{$modelName}->getDataSource();
$databaseName = $dataSource->getSchemaName();


// Do a short header
$return .= '-- Database: `' . $databaseName . '`' . "\n";
$return .= '-- Generation time: ' . date('D jS M Y H:i:s') . "\n\n\n";


if ($tables == '*') {
    $tables = array();
    $result = $this->{$modelName}->query('SHOW TABLES');
    foreach($result as $resultKey => $resultValue){
        $tables[] = current($resultValue['TABLE_NAMES']);
    }
} else {
    $tables = is_array($tables) ? $tables : explode(',', $tables);
}

// Run through all the tables
foreach ($tables as $table) {
    $tableData = $this->{$modelName}->query('SELECT * FROM ' . $table);

    $return .= 'DROP TABLE IF EXISTS ' . $table . ';';
    $createTableResult = $this->{$modelName}->query('SHOW CREATE TABLE ' . $table);
    $createTableEntry = current(current($createTableResult));
    $return .= "\n\n" . $createTableEntry['Create Table'] . ";\n\n";

    // Output the table data
    foreach($tableData as $tableDataIndex => $tableDataDetails) {

        $return .= 'INSERT INTO ' . $table . ' VALUES(';

        foreach($tableDataDetails[$table] as $dataKey => $dataValue) {

            if(is_null($dataValue)){
                $escapedDataValue = 'NULL';
            }
            else {
                // Convert the encoding
                $escapedDataValue = mb_convert_encoding( $dataValue, "UTF-8", "ISO-8859-1" );

                // Escape any apostrophes using the datasource of the model.
                $escapedDataValue = $this->{$modelName}->getDataSource()->value($escapedDataValue);
            }

            $tableDataDetails[$table][$dataKey] = $escapedDataValue;
        }
        $return .= implode(',', $tableDataDetails[$table]);

        $return .= ");\n";
    }

    $return .= "\n\n\n";
}

// Set the default file name
$fileName = $databaseName . '-backup-' . date('Y-m-d_H-i-s') . '.sql';

// Serve the file as a download
$this->autoRender = false;
$this->response->type('Content-Type: text/x-sql');
$this->response->download($fileName);
$this->response->body($return);
}

1 个答案:

答案 0 :(得分:1)

将其添加到admin_database_mysql_dump功能的最底部:

file_put_contents ( ROOT.'/app/webroot/sql/' . $fileName , $return );

这会将.sql文件下载到sql目录中的文件夹app/webroot

但是,这也会将副本下载到本地计算机。如果您不想这样做,只需删除以下行:

$this->response->download($fileName);
$this->response->body($return);

...并在生成文件后添加新行以重定向。类似于:

$this->redirect($this->referer());

因此,修改过的函数的底部应该如下所示:

// Set the default file name
$fileName = $databaseName . '-backup-' . date('Y-m-d_H-i-s') . '.sql';

// Serve the file as a download
$this->autoRender = false;
$this->response->type('Content-Type: text/x-sql');

//these two lines should be removed
// $this->response->download($fileName);
// $this->response->body($return);

//add this to write the file to server
file_put_contents ( ROOT.'/app/tmp/' . $fileName , $return );

//add this to redirect the browser to the referer
$this->redirect($this->referer());