CFile::copy PHP Method

copy() public method

Destination path supplied by user resolved to real destination path with {@link resolveDestPath}
public copy ( string $dest ) : CFile | boolean
$dest string Destination path for the current filesystem object to be copied to
return CFile | boolean New CFile object for newly created filesystem object on success, 'False' on fail.
    public function copy($dest)
    {
        $dest_realpath = $this->resolveDestPath($dest);
        if ($this->getIsFile()) {
            if ($this->getReadable() && @copy($this->_realpath, $dest_realpath)) {
                return $this->set($dest_realpath);
            }
        } else {
            $this->addLog('Copying directory "' . $this->_realpath . '" to "' . $dest_realpath . '"', 'trace');
            $contents = $this->dirContents($this->_realpath, True);
            foreach ($contents as $item) {
                $item_dest = $dest_realpath . str_replace($this->_realpath, '', $item);
                if (is_file($item)) {
                    @copy($item, $item_dest);
                } elseif (is_dir($item)) {
                    $this->createDir(0754, $item_dest);
                }
            }
            return $this->set($dest_realpath);
        }
        $this->addLog('Unable to copy filesystem object into "' . $dest_realpath . '"');
        return False;
    }

Usage Example

示例#1
0
 public function testCopy()
 {
     // A file.
     $filePath0 = CFile::createTemporary();
     $filePath1 = CFile::createTemporary();
     CFile::write($filePath0, "Hello there!");
     CFile::copy($filePath0, $filePath1);
     $this->assertTrue(CFile::exists($filePath0) && CFile::exists($filePath1) && CFile::read($filePath1)->equals("Hello there!"));
     CFile::delete($filePath0);
     CFile::delete($filePath1);
     // A directory.
     $directoryPath0 = CFilePath::add(CSystem::temporaryFilesDp(), CFile::DEFAULT_TEMPORARY_FILE_PREFIX . self::$ms_tempDirName);
     if (CFile::exists($directoryPath0)) {
         CFile::deleteDirectoryRecursive($directoryPath0);
     }
     CFile::createDirectory($directoryPath0);
     CFile::create(CFilePath::add($directoryPath0, "file-a3"));
     CFile::create(CFilePath::add($directoryPath0, "file-a20"));
     CFile::create(CFilePath::add($directoryPath0, "file-a100"));
     $directoryPath0Sub0 = CFilePath::add($directoryPath0, "dir-a2");
     $directoryPath0Sub1 = CFilePath::add($directoryPath0, "dir-a10");
     CFile::createDirectory($directoryPath0Sub0);
     CFile::create(CFilePath::add($directoryPath0Sub0, "file-a2"));
     CFile::create(CFilePath::add($directoryPath0Sub0, "file-a10"));
     CFile::createDirectory($directoryPath0Sub1);
     CFile::create(CFilePath::add($directoryPath0Sub1, "file-a2"));
     CFile::create(CFilePath::add($directoryPath0Sub1, "file-a10"));
     $directoryPath1 = "{$directoryPath0}-copy";
     if (CFile::exists($directoryPath1)) {
         CFile::deleteDirectoryRecursive($directoryPath1);
     }
     CFile::copy($directoryPath0, $directoryPath1);
     $this->assertTrue(CFile::exists($directoryPath1));
     $paths = CFile::listItemsRecursive($directoryPath1);
     $comparator = function ($arrayString, $findString) {
         return $arrayString->endsWith($findString);
     };
     $this->assertTrue($paths->length() == 9);
     $this->assertTrue($paths->find("/file-a3", $comparator) && $paths->find("/file-a20", $comparator) && $paths->find("/file-a100", $comparator) && $paths->find("/dir-a2", $comparator) && $paths->find("/dir-a10", $comparator) && $paths->find("/dir-a2/file-a2", $comparator) && $paths->find("/dir-a2/file-a10", $comparator) && $paths->find("/dir-a10/file-a2", $comparator) && $paths->find("/dir-a10/file-a10", $comparator));
     CFile::deleteDirectoryRecursive($directoryPath0);
     CFile::deleteDirectoryRecursive($directoryPath1);
 }