|
|
|
|
$username = "root" ; |
|
$password = "" ; |
|
$hostname = "localhost" ; |
|
$dbname = "cars" ; |
|
|
|
//
if mysqldump is on the system path you do not need to specify the full path |
|
//
simply use "mysqldump --add-drop-table ..." in this case |
|
$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 the dump file |
|
$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 ; |
|
} |
|
?> |
此代码不需要可写权限:
如果你没有写的权限,请使用第二个php代码,缺点是导出的sql文件不会被zip压缩。
01 |
<?php |
02 |
ob_start(); |
03 |
|
04 |
$username = "root" ; |
05 |
$password = "" ; |
06 |
$hostname = "localhost" ; |
07 |
$dbname = "cars" ; |
08 |
|
09 |
//
if mysqldump is on the system path you do not need to specify the full path |
10 |
//
simply use "mysqldump --add-drop-table ..." in this case |
11 |
$command =
"C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host= $hostname |
12 |
--user= $username "; |
13 |
if ( $password ) |
14 |
$command .= "--password=" . $password . "
" ; |
15 |
$command .= $dbname ; |
16 |
system( $command ); |
17 |
|
18 |
$dump =
ob_get_contents(); |
19 |
ob_end_clean(); |
20 |
|
21 |
//
send dump file to the output |
22 |
header( 'Content-Description:
File Transfer' ); |
23 |
header( 'Content-Type:
application/octet-stream' ); |
24 |
header( 'Content-Disposition:
attachment; filename=' . basename ( $dbname . "_" . |
25 |
date ( "Y-m-d_H-i-s" ). ".sql" )); |
26 |
flush (); |
27 |
echo $dump ; |
28 |
exit ();]]> |
29 |
?> |