php数据库备份还原函数

文章提供一款备份还原函数这是利用php写的可以备份也可以还原的函数,同时也支持备份后保存到本地的代码,好了下面看源码.

  1. //备份还原函数
  2. function write_file($sql,$filename) {
  3. $re=true;
  4. if(!@$fp=fopen("./www.phpfensi.com/".$filename,"w+")) {$re=false; echo "failed to open target file";}
  5. if(!@fwrite($fp,$sql)) {$re=false; echo "failed to write file";}
  6. if(!@fclose($fp)) {$re=false; echo "failed to close target file";}
  7. return $re;
  8. }
  9. function down_file($sql,$filename){
  10. ob_end_clean();
  11. header("content-encoding: none");
  12. header("content-type: ".(strpos($_server['http_user_agent'], 'msie') ? 'application/octetstream' : 'application/octet-stream'));
  13. header("content-disposition: ".(strpos($_server['http_user_agent'], 'msie') ? 'inline; ' : 'attachment; ')."filename=".$filename);
  14. header("content-length: ".strlen($sql));
  15. header("pragma: no-cache");
  16. header("expires: 0");
  17. echo $sql;
  18. $e=ob_get_contents();
  19. ob_end_clean();
  20. }
  21. function writeable($dir){
  22. if(!is_dir($dir)) {
  23. @mkdir($dir, 0777);
  24. }
  25. if(is_dir($dir)){
  26. if($fp = @fopen("$dir/test.test", 'w')){
  27. @fclose($fp);
  28. @unlink("$dir/test.test");
  29. $writeable = 1;
  30. }else {
  31. $writeable = 0;
  32. }
  33. }
  34. return $writeable;
  35. }
  36. function make_header($table){
  37. global $db;
  38. $sql="drop table if exists `".$table."`; ";
  39. $db->query("show create table ".$table);
  40. $db->nextrecord();
  41. $tmp=preg_replace("/ /","",$db->f("create table"));
  42. $sql.=$tmp."; ";
  43. return $sql;
  44. }
  45. function make_record($table,$num_fields){
  46. global $db;
  47. $comma="";
  48. $sql .= "insert into ".$table." values(";
  49. for($i = 0; $i < $num_fields; $i++)
  50. {$sql .= ($comma."'".mysql教程_escape_string($db->record[$i])."'"); $comma = ",";}
  51. $sql .= "); ";
  52. return $sql;
  53. }
  54. function show_msg($msgs){
  55. $i=0;
  56. $tm1="<table width='100%' cellpadding='0' cellspacing='1'><tr><td><font color='red'><b>提示信息:</b></font></td></tr><tr><td><ul>";
  57. while (list($k,$v)=each($msgs)){
  58. $i=$i+1;
  59. $t1="<li>$i.".$v."</li>";
  60. $t=$t.$t1;
  61. }
  62. $tm2="</ul></td></tr></table>";
  63. return $tm1.$t.$tm2;
  64. }
  65. function pageend(){
  66. exit();
  67. }
  68. function import($fname) {
  69. global $db;
  70. $sqls=file($fname);
  71. foreach($sqls as $sql){
  72. str_replace(" ","",$sql);
  73. str_replace(" ","",$sql);
  74. if(!$db->query(trim($sql))) return false;
  75. }//开源代码phpfensi.com
  76. return true;
  77. }