php打包备份好的数据库文件(也可打包别的文件)

简介:
 
  1. <?php 
  2. $z = new PHPZip(); 
  3. $file = array('1.jpg'); 
  4. $zipname = time() . '.zip'; 
  5. $z->Zip($file, $zipname); 
  6.  
  7. /** 
  8. 以下是其他调用这个类的方法: 
  9. $z = new PHPZip(); //新建立一个zip的类 
  10.  
  11. 方法一: 
  12. $z -> Zip("", "out1.zip"); //添加当前目录和子目录下的所有档案 
  13.  
  14. 方法二: 
  15. $files=array('1.txt','gb.txt'); 
  16. $files[]='5.txt'; 
  17. $z -> Zip($files, "out2.zip"); //添加文件列表 
  18.  
  19. 方法三: 
  20. $z -> Zip("/usr/local/sext/", "out3.zip"); //添加指定目录 
  21. */ 
  22.  
  23. class PHPZip 
  24. { 
  25. function Zip($dir, $zipfilename) 
  26. { 
  27. if (@function_exists('gzcompress')) 
  28. { 
  29. $curdir = getcwd(); 
  30. if (is_array($dir)) 
  31. { 
  32. $filelist = $dir; 
  33. } 
  34. else 
  35. { 
  36. $filelist = $this -> GetFileList($dir); 
  37. } 
  38.  
  39. if ((!emptyempty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir); 
  40. else chdir($curdir); 
  41.  
  42. if (count($filelist)>0) 
  43. { 
  44. foreach($filelist as $filename) 
  45. { 
  46. if (is_file($filename)) 
  47. { 
  48. $fd = fopen ($filename, "r"); 
  49. $content = fread ($fd, filesize ($filename)); 
  50. fclose ($fd); 
  51.  
  52. if (is_array($dir)) $filename = basename($filename); 
  53. $this -> addFile($content, $filename); 
  54. } 
  55. } 
  56. $out = $this -> file(); 
  57.  
  58. chdir($curdir); 
  59. $fp = fopen($zipfilename, "w"); 
  60. fwrite($fp, $out, strlen($out)); 
  61. fclose($fp); 
  62. } 
  63. return 1; 
  64. } 
  65. else return 0; 
  66. } 
  67.  
  68. function GetFileList($dir) 
  69. { 
  70. if (file_exists($dir)) 
  71. { 
  72. $args = func_get_args(); 
  73. $pref = $args[1]; 
  74.  
  75. $dh = opendir($dir); 
  76. while($files = readdir($dh)) 
  77. { 
  78. if (($files!=".")&&($files!="..")) 
  79. { 
  80. if (is_dir($dir.$files)) 
  81. { 
  82. $curdir = getcwd(); 
  83. chdir($dir.$files); 
  84. $file = array_merge($file, $this -> GetFileList("", "$pref$files/")); 
  85. chdir($curdir); 
  86. } 
  87. else $file[]=$pref.$files; 
  88. } 
  89. } 
  90. closedir($dh); 
  91. } 
  92. return $file; 
  93. } 
  94.  
  95. var $datasec      = array(); 
  96. var $ctrl_dir     = array(); 
  97. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; 
  98. var $old_offset   = 0; 
  99.  
  100. /** 
  101. * Converts an Unix timestamp to a four byte DOS date and time format (date 
  102. * in high two bytes, time in low two bytes allowing magnitude comparison). 
  103. * 
  104. * @param integer the current Unix timestamp 
  105. * 
  106. * @return integer the current date in a four byte DOS format 
  107. * 
  108. * @access private 
  109. */ 
  110. function unix2DosTime($unixtime = 0) { 
  111. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); 
  112.  
  113. if ($timearray['year'] < 1980) { 
  114. $timearray['year']    = 1980; 
  115. $timearray['mon']     = 1; 
  116. $timearray['mday']    = 1; 
  117. $timearray['hours']   = 0; 
  118. $timearray['minutes'] = 0; 
  119. $timearray['seconds'] = 0; 
  120. } // end if 
  121.  
  122. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | 
  123. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1); 
  124. } // end of the 'unix2DosTime()' method 
  125.  
  126.  
  127. /** 
  128. * Adds "file" to archive 
  129. * 
  130. * @param string   file contents 
  131. * @param string   name of the file in the archive (may contains the path) 
  132. * @param integer the current timestamp 
  133. * 
  134. * @access public 
  135. */ 
  136. function addFile($data, $name, $time = 0) 
  137. { 
  138. $name     = str_replace('\\', '/', $name); 
  139.  
  140. $dtime    = dechex($this->unix2DosTime($time)); 
  141. $hexdtime = '\x' . $dtime[6] . $dtime[7] 
  142. . '\x' . $dtime[4] . $dtime[5] 
  143. . '\x' . $dtime[2] . $dtime[3] 
  144. . '\x' . $dtime[0] . $dtime[1]; 
  145. eval('$hexdtime = "' . $hexdtime . '";'); 
  146.  
  147. $fr   = "\x50\x4b\x03\x04"; 
  148. $fr   .= "\x14\x00";            // ver needed to extract 
  149. $fr   .= "\x00\x00";            // gen purpose bit flag 
  150. $fr   .= "\x08\x00";            // compression method 
  151. $fr   .= $hexdtime;             // last mod time and date 
  152.  
  153. // "local file header" segment 
  154. $unc_len = strlen($data); 
  155. $crc     = crc32($data); 
  156. $zdata   = gzcompress($data); 
  157. $c_len   = strlen($zdata); 
  158. $zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug 
  159. $fr      .= pack('V', $crc);             // crc32 
  160. $fr      .= pack('V', $c_len);           // compressed filesize 
  161. $fr      .= pack('V', $unc_len);         // uncompressed filesize 
  162. $fr      .= pack('v', strlen($name));    // length of filename 
  163. $fr      .= pack('v', 0);                // extra field length 
  164. $fr      .= $name; 
  165.  
  166. // "file data" segment 
  167. $fr .= $zdata; 
  168.  
  169. // "data descriptor" segment (optional but necessary if archive is not 
  170. // served as file) 
  171. $fr .= pack('V', $crc);                 // crc32 
  172. $fr .= pack('V', $c_len);               // compressed filesize 
  173. $fr .= pack('V', $unc_len);             // uncompressed filesize 
  174.  
  175. // add this entry to array 
  176. $this -> datasec[] = $fr; 
  177. $new_offset        = strlen(implode('', $this->datasec)); 
  178.  
  179. // now add to central directory record 
  180. $cdrec = "\x50\x4b\x01\x02"; 
  181. $cdrec .= "\x00\x00";                // version made by 
  182. $cdrec .= "\x14\x00";                // version needed to extract 
  183. $cdrec .= "\x00\x00";                // gen purpose bit flag 
  184. $cdrec .= "\x08\x00";                // compression method 
  185. $cdrec .= $hexdtime;                 // last mod time & date 
  186. $cdrec .= pack('V', $crc);           // crc32 
  187. $cdrec .= pack('V', $c_len);         // compressed filesize 
  188. $cdrec .= pack('V', $unc_len);       // uncompressed filesize 
  189. $cdrec .= pack('v', strlen($name) ); // length of filename 
  190. $cdrec .= pack('v', 0 );             // extra field length 
  191. $cdrec .= pack('v', 0 );             // file comment length 
  192. $cdrec .= pack('v', 0 );             // disk number start 
  193. $cdrec .= pack('v', 0 );             // internal file attributes 
  194. $cdrec .= pack('V', 32 );            // external file attributes - 'archive' bit set 
  195.  
  196. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header 
  197. $this -> old_offset = $new_offset; 
  198.  
  199. $cdrec .= $name; 
  200.  
  201. // optional extra field, file comment goes here 
  202. // save to central directory 
  203. $this -> ctrl_dir[] = $cdrec; 
  204. } // end of the 'addFile()' method 
  205.  
  206.  
  207. /** 
  208. * Dumps out file 
  209. * 
  210. * @return string the zipped file 
  211. * 
  212. * @access public 
  213. */ 
  214. function file() 
  215. { 
  216. $data    = implode('', $this -> datasec); 
  217. $ctrldir = implode('', $this -> ctrl_dir); 
  218.  
  219. return 
  220. $data . 
  221. $ctrldir . 
  222. $this -> eof_ctrl_dir . 
  223. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk" 
  224. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall 
  225. pack('V', strlen($ctrldir)) .           // size of central dir 
  226. pack('V', strlen($data)) .              // offset to start of central dir 
  227. "\x00\x00";                             // .zip file comment length 
  228. } // end of the 'file()' method 
  229.  
  230.  
  231. } // end of the 'PHPZip' class 
  232.  
  233. ?> 

 

      本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/882336,如需转载请自行联系原作者


相关文章
|
数据库连接 应用服务中间件 PHP
|
存储 关系型数据库 分布式数据库
PolarDB开源数据库进阶课5 在线备份
本文介绍了如何在PolarDB RAC一写多读集群中进行在线备份,特别针对共享存储模式。通过使用`polar_basebackup`工具,可以将实例的本地数据和共享数据备份到本地盘中。实验环境依赖于Docker容器中用loop设备模拟的共享存储。
697 1
|
存储 SQL Java
数据存储使用文件还是数据库,哪个更合适?
数据库和文件系统各有优劣:数据库读写性能较低、结构 rigid,但具备计算能力和数据一致性保障;文件系统灵活易管理、读写高效,但缺乏计算能力且无法保证一致性。针对仅需高效存储与灵活管理的场景,文件系统更优,但其计算短板可通过开源工具 SPL(Structured Process Language)弥补。SPL 提供独立计算语法及高性能文件格式(如集文件、组表),支持复杂计算与多源混合查询,甚至可替代数据仓库。此外,SPL 易集成、支持热切换,大幅提升开发运维效率,是后数据库时代文件存储的理想补充方案。
|
存储 关系型数据库 MySQL
利用Cron表达式实现MySQL数据库的定时备份
以上就是如何使用Cron表达式和mysqldump命令实现MySQL数据库的定时备份。这种方法的优点是简单易用,而且可以根据需要定制备份的时间和频率。但是,它也有一些限制,例如,它不能备份MySQL服务器的配置文件和用户账户信息,也不能实现增量备份。如果需要更复杂的备份策略,可能需要使用专门的备份工具或服务。
619 15
|
SQL 数据库连接 Linux
数据库编程:在PHP环境下使用SQL Server的方法。
看看你吧,就像一个调皮的小丑鱼在一片广阔的数据库海洋中游弋,一路上吞下大小数据如同海中的珍珠。不管有多少难关,只要记住这个流程,剩下的就只是探索未知的乐趣,沉浸在这个充满挑战的数据库海洋中。
690 16
|
关系型数据库 Shell 网络安全
定期备份数据库:基于 Shell 脚本的自动化方案
本篇文章分享一个简单的 Shell 脚本,用于定期备份 MySQL 数据库,并自动将备份传输到远程服务器,帮助防止数据丢失。
|
Ubuntu PHP 数据库
|
存储 NoSQL MongoDB
数据库数据恢复—MongoDB数据库迁移过程中丢失文件的数据恢复案例
某单位一台MongoDB数据库由于业务需求进行了数据迁移,数据库迁移后提示:“Windows无法启动MongoDB服务(位于 本地计算机 上)错误1067:进程意外终止。”
|
关系型数据库 MySQL 数据库
数据库数据恢复—MYSQL数据库文件损坏的数据恢复案例
mysql数据库文件ibdata1、MYI、MYD损坏。 故障表现:1、数据库无法进行查询等操作;2、使用mysqlcheck和myisamchk无法修复数据库。
|
SQL 关系型数据库 MySQL
MySQL导入.sql文件后数据库乱码问题
本文分析了导入.sql文件后数据库备注出现乱码的原因,包括字符集不匹配、备注内容编码问题及MySQL版本或配置问题,并提供了详细的解决步骤,如检查和统一字符集设置、修改客户端连接方式、检查MySQL配置等,确保导入过程顺利。

热门文章

最新文章