成人午夜激情影院,小视频免费在线观看,国产精品夜夜嗨,欧美日韩精品一区二区在线播放

一鍵導出MySQL數(shù)據(jù)庫并zip壓縮的php代碼

2012-04-22 20:46:13來源:OSChina編譯作者:

有時候,你的用戶要求添加一個選項,導出整個數(shù)據(jù)庫到一個SQL文件。雖然phpMyAdmin,以及Navicat有這個功能,但你的用戶想要更簡單點怎么辦?

有時候,你的用戶要求添加一個選項,導出整個數(shù)據(jù)庫到一個SQL文件。雖然phpMyAdmin,以及Navicat有這個功能,但你的用戶想要更簡單點怎么辦?

以下是如何一鍵導出MySQL數(shù)據(jù)庫的php代碼。

新建一個名為backup.php的文件,復制粘貼以下代碼,然后編輯數(shù)據(jù)庫連接設置和mysqldump的路徑。有必要的話,你還可以添加一個backup.php超鏈接到你的程序里:

<A href="backup.php">導出整個數(shù)據(jù)庫</A>

請注意,第一個php代碼執(zhí)行的時候,會導出zip壓縮后的sql文件,所以此代碼所在文件夾需要可寫的權限。
如果你沒有寫的權限,請使用第二個php代碼,缺點是導出的sql文件不會被zip壓縮。

此代碼需要可寫權限:

<?php
 
$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
ob_start();
 
$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
$command = "C:\\xampp\\mysql\\bin\\mysqldump --add-drop-table --host=$hostname
    --user=$username ";
if ($password) 
        $command.= "--password=". $password ." "; 
$command.= $dbname;
system($command);
 
$dump = ob_get_contents(); 
ob_end_clean();
 
// send dump file to the output
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($dbname . "_" . 
    date("Y-m-d_H-i-s").".sql"));
flush();
echo $dump;
exit();]]>
?>

原文:http://webcheatsheet.com/php/how_to_create_a_dump_of_mysql_database_in_one_click.php

贊助商鏈接:

主站蜘蛛池模板: 通城县| 二手房| 张家口市| 监利县| 池州市| 中方县| 张北县| 甘肃省| 兴义市| 宁远县| 永修县| 于都县| 长子县| 石泉县| 阳城县| 海口市| 延长县| 印江| 招远市| 九龙坡区| 宾川县| 偏关县| 和政县| 保山市| 宾川县| 连州市| 湖北省| 乌拉特后旗| 冀州市| 崇文区| 抚州市| 文水县| 仙桃市| 徐水县| 临澧县| 大埔区| 青龙| 仙游县| 增城市| 剑阁县| 成安县|