diff --git a/src/Filesystem/Folder.php b/src/Filesystem/Folder.php index de87eddd80a..53151a1bae0 100644 --- a/src/Filesystem/Folder.php +++ b/src/Filesystem/Folder.php @@ -661,6 +661,7 @@ public function delete($path = null) * - `mode` The mode to copy the files/directories with as integer, e.g. 0775. * - `skip` Files/directories to skip. * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP + * - `recursive` Whether to copy recursively or not (default: true - recursive) * * @param array|string $options Either an array of options (see above) or a string of the destination directory. * @return bool Success. @@ -680,7 +681,8 @@ public function copy($options) 'from' => $this->path, 'mode' => $this->mode, 'skip' => [], - 'scheme' => Folder::MERGE + 'scheme' => Folder::MERGE, + 'recursive' => true ]; $fromDir = $options['from']; @@ -723,6 +725,10 @@ public function copy($options) $this->delete($to); } + if(is_dir($from) && $options['recursive'] === false) { + continue; + } + if (is_dir($from) && !file_exists($to)) { $old = umask(0); if (mkdir($to, $mode, true)) { @@ -763,6 +769,7 @@ public function copy($options) * - `chmod` The mode to copy the files/directories with. * - `skip` Files/directories to skip. * - `scheme` Folder::MERGE, Folder::OVERWRITE, Folder::SKIP + * - `recursive` Whether to copy recursively or not (default: true - recursive) * * @param array|string $options (to, from, chmod, skip, scheme) * @return bool Success @@ -774,7 +781,7 @@ public function move($options) $to = $options; $options = (array)$options; } - $options += ['to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => []]; + $options += ['to' => $to, 'from' => $this->path, 'mode' => $this->mode, 'skip' => [], 'recursive' => true]; if ($this->copy($options)) { if ($this->delete($options['from'])) { diff --git a/tests/TestCase/Filesystem/FolderTest.php b/tests/TestCase/Filesystem/FolderTest.php index 7e43eb17fc6..831d3f9e771 100644 --- a/tests/TestCase/Filesystem/FolderTest.php +++ b/tests/TestCase/Filesystem/FolderTest.php @@ -1030,6 +1030,24 @@ public function testCopyWithOverwrite() $this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php')); } + /** + * testCopyWithoutResursive + * + * Verify that only the files exist in the target directory. + * + * @return void + */ + public function testCopyWithoutRecursive() + { + extract($this->_setupFilesystem()); + + $Folder = new Folder($folderOne); + $result = $Folder->copy(['to' => $folderThree, 'recursive' => false]); + + $this->assertTrue(file_exists($folderThree . DS . 'file1.php')); + $this->assertFalse(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php')); + } + /** * Setup filesystem for copy tests * $path: folder_test/