2015-04-28 54 views
0

我想在下面給出的函數中設置SQL格式的數據庫備份文件的下載文件路徑。每當我嘗試這個功能,將文件下載到我的本地計算機上的下載文件夾,但我想將它下載到指定的目錄我的服務器上,而不是:在CakePHP中設置下載數據庫備份的文件路徑

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); 
} 
+0

你希望它是在服務器本身? – Abhishek

+0

我只想將下載位置更改爲我的其他目錄而不是下載文件夾(默認) – tarun

+0

從瀏覽器保存文件的位置基於瀏覽器本身 – Abhishek

回答

1

添加到您的admin_database_mysql_dump的最底部功能:

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

這會在你的app/webroot目錄中的.sql文件下載到的文件夾sql

但是,這也會將副本下載到本地計算機。如果您不想這樣做,只需刪除以下行:

$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()); 
+0

這是否解決了您的問題? – McWayWeb

相關問題