2014-02-20 156 views
0

當MySQL工作,我用這個代碼來獲取數據庫的備份:mysqli的數據庫備份

if (isset($_POST['getbackup'])) { 

    $db = "newsite"; 
    $version = "2.3.1"; 
    $date = date('Y-m-d H:i:s'); 

    $final = "-- #".$version."#\n"; 
    $final .= "-- database backup\n"; 
    $final .= "--\n"; 
    $final .= "-- PHP version: ".phpversion()."\n"; 
    $final .= "-- MySQL version: ".mysqli_get_server_info($mysqli)."\n"; 
    $final .= "-- Date: ".date("r")."\n"; 

    $result = $mysqli->query("SHOW TABLE STATUS FROM ".$db); 
    while ($table = $result->fetch_array()) { 
    $i = 0; 
    $result2 = $mysqli->query("SHOW COLUMNS FROM $table[0]"); 
    $z = $result2->num_rows; 
    $final .= "\n--\n-- DB Export - Table structure for table `".$table[0]."`\n--\n\nCREATE TABLE `".$table[0]."` ("; 
    $prikey = false; 
    $insert_keys = null; 
    while ($row2 = $result2->fetch_array()) { 
     $i++; 
     $insert_keys .="`".$row2['Field']."`"; 
     $final .= "`".$row2['Field']."` ".$row2['Type']; 
     if($row2['Null'] != "YES") { $final .= " NOT NULL"; } 
     if($row2['Default']) $final .= " DEFAULT '".$row2['Default']."'"; 
     if($row2['Extra']) { $final .= " ".$row2['Extra']; } 
     if($row2['Key'] == "PRI") { $final .= ", PRIMARY KEY (`".$row2['Field']."`)"; $prikey = true; } 
     if($i < $z){ 
      $final .= ", "; 
      $insert_keys .=", "; 
     } 
     else{ 
      $final .= " "; 
     } 
    } 
    if($prikey) { 
     if($table[10]) $auto_inc = " AUTO_INCREMENT=".$table[10]; 
     else $auto_inc = " AUTO_INCREMENT=1"; 
    } 
    else $auto_inc = ""; 
    $charset = explode("_", $table[14]); 
    $final .= ") ENGINE=".$table[1]." DEFAULT CHARSET=".$charset[0]." COLLATE=".$table[14].$auto_inc.";\n\n--\n-- DB Export - Dumping data for table `".$table[0]."`\n--\n"; 

    $inhaltq = $mysqli->query("SELECT * FROM $table[0]"); 
    while($inhalt = $inhaltq->fetch_array()) { 
     $final .= "\nINSERT INTO `$table[0]` ("; 
     $final .= $insert_keys; 
     $final .= ") VALUES ("; 
     for($i=0;$i<$z;$i++) { 

      $inhalt[$i] = str_replace("'","`", $inhalt[$i]); 
      $inhalt[$i] = str_replace("\\","\\\\", $inhalt[$i]); 
      $einschub = "'".$inhalt[$i]."'"; 
      $final .= preg_replace('/\r\n|\r|\n/', '\r\n', $einschub); 
      if(($i+1)<$z) $final .= ", "; 

     } 
     $final .= ");"; 
    } 
    $final .= "\n"; 
} 
if($logged) { 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Content-Type: application/force-download"); 
    header("Content-Description: File Transfer"); 
    if(is_integer(mb_strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "msie")) AND is_integer(mb_strpos(strtolower($_SERVER["HTTP_USER_AGENT"]), "win"))) header("Content-Disposition: filename=backup-".strtolower(date("D-d-M-Y")).".sql;"); 
    else header("Content-Disposition: attachment; filename=backup-".strtolower(date("D-d-M-Y")).".sql;"); 
    header("Content-Transfer-Encoding: binary"); 
} 

} 

它現在還有,我得到一個文件時,我點擊下載按鈕,但文件中沒有SQL文件,而是頁面的源代碼在文件中。

我在做什麼錯?

+0

所以,你說的是,網站上的,PHP文件將無法解析,而是他們的來源只是出現?還是隻是這一個文件? – Carsten

回答

1

您並未輸出您的$final數據。我使用此代碼做同樣的事情,嘗試...

header('Content-Description: File Transfer'); 
header('Content-type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=' . $backup_filename . '.sql'); 
header('Content-Transfer-Encoding: binary'); 
header('Content-Length: ' . strlen($final)); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Expires: 0'); 
header('Pragma: public'); 
echo $final; 
+0

啊,謝謝@PeteR,一開始確實有,但我刪除它,我認爲和錯過了:P好東西,你看到^^ – Tommy

+0

@湯米沒有問題。 –