/** * Copy a file to the target location. * @param $new string The new file path * @throws File\Exception\FileNotWritable * @throws File\Exception\DirectoryNotWritable * @throws File\Exception\CopyFailed * @return File The new file object */ public function copy($new) { if (is_dir($new)) { if (!is_writable($new)) { throw new DirectoryNotWritable('The directory "' . $new . '" to which "' . $this->path . '" should be copied is not writable'); } if (substr($new, strlen($new) - 1) !== '/') { $new .= '/'; } $new .= $this->filename; } $file = new File($new); if ($file->exists() && !$file->isWritable()) { throw new FileNotWritable('Target file is not writable: "' . $this->path); } $this->close(); if (!copy($this->path, $new)) { throw new CopyFailed('Could not copy "' . $this->path . '" to "' . $new . '"'); } return $file; }
public function testCopy() { $someFile = vfsStream::newFile('old.txt', 0777); $this->root->addChild($someFile); $subdir = vfsStream::newDirectory('subdir'); $this->root->addChild($subdir); $file = new File($someFile->url()); $newFile = $file->copy($subdir->url()); $this->assertTrue(file_exists($newFile->getPath())); $this->assertTrue(file_exists($file->getPath())); }