PHP for backing up the database zip and export _php tutorial

Source: Internet
Author: User
Tags ziparchive
Often in some of the PHP open source system, see the backup database and export methods, in fact, the code is not complex, the following
About the following, take windows as an example, two kinds of methods, one is the directory folder to execute script permissions,
There are no permissions, the code is as follows:

A

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 ";
$command = "C:\\xampp\\mysql\\bin\\mysqldump--add-drop-table--host= $hostname
--user= $username ";
if ($password)
$command. = "--password=". $password. " ";
$command. = $dbname;
$command. = ">". $dumpfname;
System ($command);
17.
.//Zip data file
$zipfname = $dbname. "_" . Date ("Y-m-d_h-i-s"). ". Zip ";
$zip = new Ziparchive ();
if ($zip->open ($zipfname, ziparchive::create))
22. {
$zip->addfile ($dumpfname, $dumpfname);
$zip->close ();
25.}
26.
//Read zip file and send it to standard output
if (file_exists ($zipfname)) {
Header (' Content-description:file Transfer ');
The header (' Content-type:application/octet-stream ');
Header (' content-disposition:attachment; Filename= '. basename ($zipfname));
Flush ();
ReadFile ($zipfname);
. exit;
35.}
?>.


Method 2 folder does not have the relevant 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
--user= $username ";
One. if ($password)
$command. = "--password=". $password. " ";
$command. = $dbname;
System ($command);
15.
$dump = Ob_get_contents ();
Ob_end_clean ();
18.
19.
20.//No Zip
Header (' Content-description:file Transfer ');
Header (' Content-type:application/octet-stream ');
The header (' content-disposition:attachment; Filename= '. basename ($dbname. "_" .
Date ("Y-m-d_h-i-s"). ". SQL "));
Flush ();
Echo $dump;
Exit ();]] >
?>.

Excerpted from Jackyrong

http://www.bkjia.com/PHPjc/478285.html www.bkjia.com true http://www.bkjia.com/PHPjc/478285.html techarticle often in some of the PHP open source system, see the backup database and export methods, in fact, the code is not complex, the following is probably explained below, with Windows as an example, two kinds of methods, one is 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.