PHP递归备份脚本

前端之家收集整理的这篇文章主要介绍了PHP递归备份脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我为我的网站写了一个基本的内容管理系统,包括一个管理面板.我理解基本文件IO以及通过 PHP进行复制,但是我对从脚本调用的备份脚本的尝试失败了.我试过这样做:
  1. //... authentication,other functions
  2. for(scandir($homedir) as $buffer){
  3. if(is_dir($buffer)){
  4. //Add $buffer to an array
  5. }
  6. else{
  7. //Back up the file
  8. }
  9. }
  10. for($founddirectories as $dir){
  11. for(scandir($dir) as $b){
  12. //Backup as above,adding to $founddirectories
  13. }
  14. }

但它似乎没有用.

我知道我可以使用FTP来做到这一点,但我想要一个完全服务器端的解决方案,只要有足够的授权就可以在任何地方访问.

这是另一种选择:你为什么不 Zip the source directory instead
  1. function Zip($source,$destination)
  2. {
  3. if (extension_loaded('zip') === true)
  4. {
  5. if (file_exists($source) === true)
  6. {
  7. $zip = new ZipArchive();
  8.  
  9. if ($zip->open($destination,ZIPARCHIVE::CREATE) === true)
  10. {
  11. $source = realpath($source);
  12.  
  13. if (is_dir($source) === true)
  14. {
  15. $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source),RecursiveIteratorIterator::SELF_FIRST);
  16.  
  17. foreach ($files as $file)
  18. {
  19. $file = realpath($file);
  20.  
  21. if (is_dir($file) === true)
  22. {
  23. $zip->addEmptyDir(str_replace($source . '/','',$file . '/'));
  24. }
  25.  
  26. else if (is_file($file) === true)
  27. {
  28. $zip->addFromString(str_replace($source . '/',$file),file_get_contents($file));
  29. }
  30. }
  31. }
  32.  
  33. else if (is_file($source) === true)
  34. {
  35. $zip->addFromString(basename($source),file_get_contents($source));
  36. }
  37. }
  38.  
  39. return $zip->close();
  40. }
  41. }
  42.  
  43. return false;
  44. }

您甚至可以将其解压缩并存档相同的效果,但我必须说我更喜欢以zip文件格式压缩我的备份.

猜你在找的PHP相关文章