CFile::rename PHP Method

rename() public method

Destination path supplied by user resolved to real destination path with {@link resolveDestPath}
public rename ( string $dest ) : CFile | boolean
$dest string Destination path for the current filesystem object to be renamed/moved to
return CFile | boolean Updated current CFile object on success, 'False' on fail.
    public function rename($dest)
    {
        $dest_realpath = $this->resolveDestPath($dest);
        if ($this->getWriteable() && @rename($this->_realpath, $dest_realpath)) {
            $this->_filepath = $dest;
            $this->_realpath = $dest_realpath;
            // Update pathinfo properties.
            $this->pathInfo();
            return $this;
        }
        $this->addLog('Unable to rename/move filesystem object into "' . $dest_realpath . '"');
        return False;
    }

Usage Example

Example #1
0
 public function testRename()
 {
     $filePath0 = CFile::createTemporary();
     $newName = CFilePath::name($filePath0) . "-renamed";
     $filePath1 = CFilePath::add(CFilePath::directory($filePath0), $newName);
     CFile::write($filePath0, "Hello there!");
     CFile::rename($filePath0, $newName);
     $this->assertTrue(!CFile::exists($filePath0) && CFile::exists($filePath1) && CFile::read($filePath1)->equals("Hello there!"));
     CFile::delete($filePath1);
 }