用php實現備份資料庫ZIP及匯出

來源:互聯網
上載者:User
經常在有的PHP開源系統中,看到有備份資料庫並匯出的方法,其實代碼不複雜,下面
大概講解下,以WINDOWS為例子,兩類方法,一個是目錄檔案夾要有執行指令碼許可權的,
一個個是沒有許可權的,代碼如下:

一)

<?php       $username = "root";    $password = "";    $hostname = "localhost";    $dbname   = "test";         $dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";   $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname       --user=$username ";   if ($password)            $command.= "--password=". $password ." ";    $command.= $dbname;   $command.= " > " . $dumpfname;   system($command);       // zip 資料檔案  $zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";   $zip = new ZipArchive();   if($zip->open($zipfname,ZIPARCHIVE::CREATE))    {      $zip->addFile($dumpfname,$dumpfname);      $zip->close();   }       // read zip file and send it to standard output   if (file_exists($zipfname)) {       header('Content-Description: File Transfer');       header('Content-Type: application/octet-stream');       header('Content-Disposition: attachment; filename='.basename($zipfname));       flush();       readfile($zipfname);       exit;   }   ?>



方法2 檔案夾沒相關許可權

<?php   ob_start();       $username = "root";    $password = "";    $hostname = "localhost";    $dbname   = "test";       $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname       --user=$username ";   if ($password)            $command.= "--password=". $password ." ";    $command.= $dbname;   system($command);       $dump = ob_get_contents();    ob_end_clean();         //不ZIP了  header('Content-Description: File Transfer');   header('Content-Type: application/octet-stream');   header('Content-Disposition: attachment; filename='.basename($dbname . "_" .        date("Y-m-d_H-i-s").".sql"));   flush();   echo $dump;   exit();]]>   ?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.