Home > Backend Development > PHP Tutorial > PHP copy entire folder recursively

PHP copy entire folder recursively

WBOY
Release: 2016-07-25 09:06:39
Original
1489 people have browsed it
Use PHP to recursively implement a class that copies an entire folder
  1. /*
  2. * Folder copy class,
  3. * Zhao Chun June 14, 2012 17:20:30
  4. * Blog: www.zhaochun.net
  5. */
  6. class CopyFile
  7. {
  8. public $fromFile;
  9. public $toFile;
  10. /*
  11. * $fromFile Who to copy
  12. * $toFile Copy to that
  13. */
  14. function copyFile($fromFile,$toFile){
  15. $this->CreateFolder($ toFile);
  16. $folder1=opendir($fromFile);
  17. while($f1=readdir($folder1)){
  18. if($f1!="." && $f1!=".."){
  19. $path2 ="{$fromFile}/{$f1}";
  20. if(is_file($path2)){
  21. $file = $path2;
  22. $newfile = "{$toFile}/{$f1}";
  23. copy($ file, $newfile);
  24. }elseif(is_dir($path2)){
  25. $toFiles = $toFile.'/'.$f1;
  26. $this->copyFile($path2,$toFiles);
  27. }
  28. }
  29. }
  30. }
  31. /*
  32. * Recursively create folders
  33. */
  34. function CreateFolder($dir, $mode = 0777){
  35. if (is_dir($dir) || @mkdir($dir,$mode)){
  36. return true;
  37. }
  38. if (!$this->CreateFolder(dirname($dir),$mode)){
  39. return false;
  40. }
  41. return @mkdir($dir, $mode);
  42. }
  43. }
  44. //Usage method
  45. //Introduce this class, directly new copyFile('Who to copy', 'Copy to that');
  46. //$file = new CopyFile('aaaa/aaaaa','bbbbb/bbbb') ;
  47. ?>
Copy code
PHP copy entire folder recursively PHP copy entire folder recursively


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template