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 ...