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

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

一)

Java代碼
1. 2.
3. $username = "root";
4. $password = "";
5. $hostname = "localhost";
6. $dbname = "test";
7.
8.
9. $dumpfname = $dbname . "_" . date("Y-m-d_H-i-s").".sql";
10. $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
11. --user=$username ";
12. if ($password)
13. $command.= "--password=". $password ." ";
14. $command.= $dbname;
15. $command.= " > " . $dumpfname;
16. system($command);
17.
18. // zip 資料檔案
19. $zipfname = $dbname . "_" . date("Y-m-d_H-i-s").".zip";
20. $zip = new ZipArchive();
21. if($zip->open($zipfname,ZIPARCHIVE::CREATE))
22. {
23. $zip->addFile($dumpfname,$dumpfname);
24. $zip->close();
25. }
26.
27. // read zip file and send it to standard output
28. if (file_exists($zipfname)) {
29. header('Content-Description: File Transfer');
30. header('Content-Type: application/octet-stream');
31. header('Content-Disposition: attachment; filename='.basename($zipfname));
32. flush();
33. readfile($zipfname);
34. exit;
35. }
36. ?>


方法2 檔案夾沒相關許可權
Java代碼
1. 2. ob_start();
3.
4. $username = "root";
5. $password = "";
6. $hostname = "localhost";
7. $dbname = "test";
8.
9. $command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
10. --user=$username ";
11. if ($password)
12. $command.= "--password=". $password ." ";
13. $command.= $dbname;
14. system($command);
15.
16. $dump = ob_get_contents();
17. ob_end_clean();
18.
19.
20. //不ZIP了
21. header('Content-Description: File Transfer');
22. header('Content-Type: application/octet-stream');
23. header('Content-Disposition: attachment; filename='.basename($dbname . "_" .
24. date("Y-m-d_H-i-s").".sql"));
25. flush();
26. echo $dump;
27. exit();]]>
28. ?>

摘自 jackyrong

http://www.bkjia.com/PHPjc/478285.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478285.htmlTechArticle經常在有的PHP開源系統中,看到有備份資料庫並匯出的方法,其實代碼不複雜,下面 大概講解下,以WINDOWS為例子,兩類方法,一個是目錄...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.