phpseclib\Net\SFTP::rename PHP Method

rename() public method

Renames a file or a directory on the SFTP server
public rename ( string $oldname, string $newname ) : boolean
$oldname string
$newname string
return boolean
    function rename($oldname, $newname)
    {
        if (!($this->bitmap & SSH2::MASK_LOGIN)) {
            return false;
        }
        $oldname = $this->_realpath($oldname);
        $newname = $this->_realpath($newname);
        if ($oldname === false || $newname === false) {
            return false;
        }
        // http://tools.ietf.org/html/draft-ietf-secsh-filexfer-13#section-8.3
        $packet = pack('Na*Na*', strlen($oldname), $oldname, strlen($newname), $newname);
        if (!$this->_send_sftp_packet(NET_SFTP_RENAME, $packet)) {
            return false;
        }
        $response = $this->_get_sftp_packet();
        if ($this->packet_type != NET_SFTP_STATUS) {
            throw new \UnexpectedValueException('Expected SSH_FXP_STATUS');
        }
        // if $status isn't SSH_FX_OK it's probably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED
        if (strlen($response) < 4) {
            return false;
        }
        extract(unpack('Nstatus', Strings::shift($response, 4)));
        if ($status != NET_SFTP_STATUS_OK) {
            $this->_logError($response, $status);
            return false;
        }
        // don't move the stat cache entry over since this operation could very well change the
        // atime and mtime attributes
        //$this->_update_stat_cache($newname, $this->_query_stat_cache($oldname));
        $this->_remove_from_stat_cache($oldname);
        $this->_remove_from_stat_cache($newname);
        return true;
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function move($remoteSource, $remoteDestination)
 {
     if ($this->isConnected()) {
         return $this->connection->rename($remoteSource, $remoteDestination);
     }
     return false;
 }
All Usage Examples Of phpseclib\Net\SFTP::rename