public function copy(Directory $destination, $fileOverwrite = true) { $self = $this->getPathname(); $dest = $destination->getPathname() . DIRECTORY_SEPARATOR . $this->getBasename(); foreach ($this->getIterator() as $file) { $old = $file->getPathname(); $new = substr($old, strlen($self)); $new = $dest . $new; $base = dirname($new); if ($file instanceof Directory) { static::create($new); continue; } if (!is_file($new) || $fileOverwrite) { if (!@copy($old, $new)) { throw new RuntimeException('File ' . $old . ' could not be copied to ' . $new . '.'); } } elseif (is_file($new) && !$fileOverwrite) { throw new RuntimeException('File ' . $new . ' already exists.'); } } return $this; }