/** * @depends testExist * @return void */ public function testCopy() { // create file $filePath = __DIR__ . '/tmp/example_copy.txt'; if (!is_file($filePath)) { fclose(fopen($filePath, 'x')); } // create target dir $newPath = __DIR__ . '/tmp/target'; if (!is_dir($newPath)) { mkdir($newPath, 0777); } // remove copied file $filePathNew = $newPath . '/example_copy.txt'; if (is_file($filePathNew)) { unlink($filePathNew); } $fileObject = new File($filePath); $this->assertTrue($fileObject->isFile()); $this->assertTrue($fileObject->isWritable()); $this->assertTrue($fileObject->copy($newPath)); $this->assertEquals($filePathNew, $fileObject->getPathname()); $this->assertTrue($fileObject->isFile()); // create dir $filePath = __DIR__ . '/tmp/ExampleCopy'; if (!is_dir($filePath)) { mkdir($filePath, 0777); } // remove copied file $filePathNew = $newPath . '/ExampleCopy'; if (is_dir($filePathNew)) { rmdir($filePathNew); } $fileObject = new File($filePath); $this->assertTrue($fileObject->isDir()); $this->assertTrue($fileObject->isWritable()); $this->assertTrue($fileObject->copy($newPath)); $this->assertEquals($filePathNew, $fileObject->getPathname()); $this->assertTrue($fileObject->isDir()); }
if (!$newDirectoryFileObject->isDir()) { $newDirectoryFileObject->mkdirs(); } echo 'Copy file to target directory'; echo '<br/>'; $examplePath = __DIR__ . '/example.txt'; $exampleCopyPath = __DIR__ . '/tmp/target/'; $fileObject = new File($examplePath); $fileObject->copy($exampleCopyPath); $fileObject->rename('example_copy.txt'); echo 'Move copied file to a new directory'; echo '<br/>'; $exampleMovePath = __DIR__ . '/tmp/target/move/'; $fileObject->move($exampleMovePath); $fileObject->rename('example_move.txt'); echo 'Moved File ' . $fileObject->getPathname() . ' do' . ($fileObject->exists() ? '' : ' not') . ' exist.'; echo '<br/>'; echo 'File is' . ($fileObject->isReadable() ? '' : ' not') . ' readable.'; echo '<br/>'; echo 'File is' . ($fileObject->isWritable() ? '' : ' not') . ' writeable.'; echo '<br/>'; echo 'File permission: ' . $fileObject->getPermission(); echo '<br/>'; echo '<br/>'; echo 'Remove file'; echo '<br/>'; $deletePath = __DIR__ . '/tmp/example.txt'; $fileObject = new File($deletePath); $fileObject->delete(); echo 'Remove directories'; echo '<br/>';
/** * copy file to a given directory * * @param FileInterface|string * @return bool */ public function copy($pathname) { if (!empty($pathname)) { if ($pathname instanceof FileInterface) { $targetFile = $pathname; } else { $targetFile = new File($pathname); } if ($targetFile->isDir()) { $targetPathname = $targetFile->getPathname() . self::PATH_SEPARATOR . $this->getBasename(); if ($this->isFile()) { if (copy($this->getPathname(), $targetPathname)) { parent::__construct($targetPathname); return true; } } elseif ($this->isDir()) { if ($this->copyAction($this->getPathname(), $targetPathname, true)) { parent::__construct($targetPathname); return true; } } else { throw new FileException('Copy failed, source file or directory do not exist.'); } } else { throw new FileException('Copy failed, target directory do not exist.'); } } else { throw new FileException('Copy failed, because given filepath is empty.'); } return false; }