2017-04-25 151 views
0

這是我downloding它的正常工作數據庫的壓縮文件的功能,但我想在數據庫中插入這一點,也通過動態選擇SQL文件導入數據庫

public function db_backup(){ 
    date_default_timezone_set('Asia/Calcutta'); 
    $this->load->dbutil(); 
    $prefs = array( 
     'format' => 'zip', 
     'add_drop' => TRUE, 
     'add_insert'=> TRUE, 
     'newline' => "\n" 
    ); 
    $backup = $this->dbutil->backup($prefs); 
    $this->load->helper('file'); 
    $this->load->helper('download'); 
    $this->zip->archive(FCPATH.'backup/database/'.date('Y-m-d-His').'DbBackup.zip'); 
} 

please provide me solution i tried vaious examples nothing works 

public function restoredb($id = ''){ 


    $isi_file = file_get_contents('./backups/XXXXXXX.sql'); #here I want to give dynamic path 
    $query = rtrim($isi_file, "\n;"); 
    $array_query = explode(";", $query); 

    foreach($array_query as $query){ 
     $this->db->query($query); 
    } 

} 

將其導入到數據庫以上功能恢復數據庫,但沒有工作,因爲我想

+1

你可以訪問你的PHP配置了shell_exec? –

+0

你能告訴如何訪問? – eliana

回答

1
$backup = read_file('path/to/file.sql'); 
$sql_clean = ''; 
    foreach (explode("\n", $backup) as $line){ 
     if(isset($line[0]) && $line[0] != "#"){ 
      $sql_clean .= $line."\n"; 
     } 
    } 
    foreach (explode(";\n", $sql_clean) as $sql){ 
     $sql = trim($sql); 
     if($sql){ 
      $this->db->query($sql); 
     } 
    } 

試試這個,我希望它幫

1

試試這個出口和進口數據

當出口

$cmd = "mysqldump -u $DBUSER --password=$DBPASSWD -h$HOST $DATABASE > backup.sql"; 
     shell_exec($cmd); 

導入/恢復

$cmd = "mysql -u $DBUSER --password=$DBPASSWD -h$HOST -D$database < backup.sql"; 
     shell_exec($cmd); 
+0

如何在codeigniter中使用它我試過它不起作用 – eliana

+0

它向你扔什麼錯誤? –

+0

除空白屏外 – eliana