Php-based backup database ZIP and export-PHP Tutorial

Source: Internet
Author: User
Tags ziparchive
Use php to ZIP and export the backup database. In some open-source PHP systems, we often see Methods for backing up and exporting databases. In fact, the code is not complicated. The following describes the two methods, taking WINDOWS as an example, one is that directories are often found in some PHP open-source systems and there are methods for backing up databases and exporting them. in fact, the code is not complex. below
Take WINDOWS as an example. There are two methods: One is that the directory folder must have the permission to execute scripts,
The code is as follows:

1)

Java code
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 data file
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.?>


Method 2: The folder has no related permissions.
Java code
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. // no 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.?>

From jackyrong

WINDOWS is used as an example. There are two methods: Directory...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.