Use PHP to recursively implement a class that copies an entire folder
- /*
- * Folder copy class,
- * Zhao Chun June 14, 2012 17:20:30
- * Blog: www.zhaochun.net
- */
- class CopyFile
- {
- public $fromFile;
- public $toFile;
- /*
- * $fromFile Who to copy
- * $toFile Copy to that
- */
- function copyFile($fromFile,$toFile){
- $this->CreateFolder($ toFile);
- $folder1=opendir($fromFile);
- while($f1=readdir($folder1)){
- if($f1!="." && $f1!=".."){
- $path2 ="{$fromFile}/{$f1}";
- if(is_file($path2)){
- $file = $path2;
- $newfile = "{$toFile}/{$f1}";
- copy($ file, $newfile);
- }elseif(is_dir($path2)){
- $toFiles = $toFile.'/'.$f1;
- $this->copyFile($path2,$toFiles);
- }
- }
- }
- }
- /*
- * Recursively create folders
- */
- function CreateFolder($dir, $mode = 0777){
- if (is_dir($dir) || @mkdir($dir,$mode)){
- return true;
- }
- if (!$this->CreateFolder(dirname($dir),$mode)){
- return false;
- }
- return @mkdir($dir, $mode);
- }
- }
- //Usage method
- //Introduce this class, directly new copyFile('Who to copy', 'Copy to that');
- //$file = new CopyFile('aaaa/aaaaa','bbbbb/bbbb') ;
- ?>
Copy code
|