search
Homephp教程php手册用php实现备份数据库ZIP及导出

用php实现备份数据库ZIP及导出

Jun 13, 2016 am 10:48 AM
phpzipbackupaccomplishExportOpen sourcedatabaseuselooksystem

经常在有的PHP开源系统中,看到有备份数据库并导出的方法,其实代码不复杂,下面
大概讲解下,以WINDOWS为例子,两类方法,一个是目录文件夹要有执行脚本权限的,
一个个是没有权限的,代码如下:

一)
  
Java代码    
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 数据文件  
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. ?>  


方法2 文件夹没相关权限
Java代码    
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. //不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. ?> 

 

摘自 jackyrong
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor