public function recursiveCopy($src, $dst, $blacklist = array())
{
if (!is_dir($dst)) {
@mkdir($dst, 0777, true);
}
if (!is_dir($dst)) {
throw new RuntimeException(sprintf('Destination directory <%s> error', $dst));
}
$handle = opendir($src);
if (!$handle) {
throw new RuntimeException(sprintf('Source directory <%s> error', $src));
}
$skip = array_merge(array(".", ".."), $blacklist);
$stack = array();
while (false !== ($file = readdir($handle))) {
if (in_array($file, $skip)) {
continue;
}
if (is_dir($src . '/' . $file)) {
$stack[] = $file;
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}
closedir($handle);
foreach ($stack as $file) {
$this->recursiveCopy($src . '/' . $file, $dst . '/' . $file, $blacklist);
}
}
public function testRecursiveCopyWithBlacklist() { $tmp = sys_get_temp_dir(); $basePath = $tmp . "/n98_testdir"; $folder1 = $basePath . "/folder1"; $folder2 = $basePath . "/folder2"; $file1 = $folder1 . "/file1.txt"; $ignoreMe = $folder1 . "/ignore.me"; $file2 = $folder2 . "/file2.txt"; $dest = sys_get_temp_dir() . "/n98_copy_dest"; @mkdir($folder1, 0777, true); @mkdir($folder2, 0777, true); touch($file1); touch($file2); $this->fileSystem->recursiveCopy($basePath, $dest, array('ignore.me')); $this->assertFileExists($dest . "/folder1/file1.txt"); $this->assertFileExists($dest . "/folder2/file2.txt"); $this->assertFileNotExists($dest . "/folder1/ignore.me"); //cleanup unlink($file1); unlink($file2); rmdir($folder1); rmdir($folder2); rmdir($basePath); unlink($dest . "/folder1/file1.txt"); unlink($dest . "/folder2/file2.txt"); rmdir($dest . "/folder1"); rmdir($dest . "/folder2"); rmdir($dest); }