phpseclib\Net\SFTP::chmod PHP Method

chmod() public method

Returns the new file permissions on success or false on error. If $recursive is true than this just returns true or false.
public chmod ( integer $mode, string $filename, boolean $recursive = false ) : mixed
$mode integer
$filename string
$recursive boolean
return mixed
    function chmod($mode, $filename, $recursive = false)
    {
        if (is_string($mode) && is_int($filename)) {
            $temp = $mode;
            $mode = $filename;
            $filename = $temp;
        }
        $attr = pack('N2', NET_SFTP_ATTR_PERMISSIONS, $mode & 07777);
        if (!$this->_setstat($filename, $attr, $recursive)) {
            return false;
        }
        if ($recursive) {
            return true;
        }
        $filename = $this->_realPath($filename);
        // rather than return what the permissions *should* be, we'll return what they actually are.  this will also
        // tell us if the file actually exists.
        // incidentally, SFTPv4+ adds an additional 32-bit integer field - flags - to the following:
        $packet = pack('Na*', strlen($filename), $filename);
        if (!$this->_send_sftp_packet(NET_SFTP_STAT, $packet)) {
            return false;
        }
        $response = $this->_get_sftp_packet();
        switch ($this->packet_type) {
            case NET_SFTP_ATTRS:
                $attrs = $this->_parseAttributes($response);
                return $attrs['permissions'];
            case NET_SFTP_STATUS:
                $this->_logError($response);
                return false;
        }
        throw new \UnexpectedValueException('Expected SSH_FXP_ATTRS or SSH_FXP_STATUS');
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function changePermissions($remoteTarget, $fileMode, $recursive = false)
 {
     if ($this->isConnected()) {
         $result = $this->connection->chmod($fileMode, $remoteTarget, $recursive);
         return $result !== false;
     }
     return false;
 }
All Usage Examples Of phpseclib\Net\SFTP::chmod