phpseclib\Net\SFTP::touch PHP Method

touch() public method

If the file does not exist, it will be created.
public touch ( string $filename, integer $time = null, integer $atime = null ) : boolean
$filename string
$time integer
$atime integer
return boolean
    function touch($filename, $time = null, $atime = null)
    {
        if (!($this->bitmap & SSH2::MASK_LOGIN)) {
            return false;
        }
        $filename = $this->_realpath($filename);
        if ($filename === false) {
            return false;
        }
        if (!isset($time)) {
            $time = time();
        }
        if (!isset($atime)) {
            $atime = $time;
        }
        $flags = NET_SFTP_OPEN_WRITE | NET_SFTP_OPEN_CREATE | NET_SFTP_OPEN_EXCL;
        $attr = pack('N3', NET_SFTP_ATTR_ACCESSTIME, $time, $atime);
        $packet = pack('Na*Na*', strlen($filename), $filename, $flags, $attr);
        if (!$this->_send_sftp_packet(NET_SFTP_OPEN, $packet)) {
            return false;
        }
        $response = $this->_get_sftp_packet();
        switch ($this->packet_type) {
            case NET_SFTP_HANDLE:
                return $this->_close_handle(substr($response, 4));
            case NET_SFTP_STATUS:
                $this->_logError($response);
                break;
            default:
                throw new \UnexpectedValueException('Expected SSH_FXP_HANDLE or SSH_FXP_STATUS');
        }
        return $this->_setstat($filename, $attr, false);
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function createFile($remoteFilename, $fileMode = 0770)
 {
     if ($this->isConnected()) {
         return $this->connection->touch($remoteFilename) && $this->changePermissions($remoteFilename, $fileMode);
     }
     return false;
 }