phpseclib\Net\SFTP::lstat PHP Method

lstat() public method

Returns an array on success and false otherwise.
public lstat ( string $filename ) : mixed
$filename string
return mixed
    function lstat($filename)
    {
        if (!($this->bitmap & SSH2::MASK_LOGIN)) {
            return false;
        }
        $filename = $this->_realpath($filename);
        if ($filename === false) {
            return false;
        }
        if ($this->use_stat_cache) {
            $result = $this->_query_stat_cache($filename);
            if (is_array($result) && isset($result['.']) && isset($result['.']->lstat)) {
                return $result['.']->lstat;
            }
            if (is_object($result) && isset($result->lstat)) {
                return $result->lstat;
            }
        }
        $lstat = $this->_stat($filename, NET_SFTP_LSTAT);
        if ($lstat === false) {
            $this->_remove_from_stat_cache($filename);
            return false;
        }
        if (isset($lstat['type'])) {
            if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) {
                $filename .= '/.';
            }
            $this->_update_stat_cache($filename, (object) array('lstat' => $lstat));
            return $lstat;
        }
        $stat = $this->_stat($filename, NET_SFTP_STAT);
        if ($lstat != $stat) {
            $lstat = array_merge($lstat, array('type' => NET_SFTP_TYPE_SYMLINK));
            $this->_update_stat_cache($filename, (object) array('lstat' => $lstat));
            return $stat;
        }
        $pwd = $this->pwd;
        $lstat['type'] = $this->chdir($filename) ? NET_SFTP_TYPE_DIRECTORY : NET_SFTP_TYPE_REGULAR;
        $this->pwd = $pwd;
        if ($lstat['type'] == NET_SFTP_TYPE_DIRECTORY) {
            $filename .= '/.';
        }
        $this->_update_stat_cache($filename, (object) array('lstat' => $lstat));
        return $lstat;
    }

Usage Example

Esempio n. 1
0
 /**
  * Copies a file/directory on the remote host
  *
  * @param  string            $src
  * @param  string            $dest
  * @param  bool              $recursive
  * @throws \RuntimeException
  * @return mixed
  */
 public function copy($src, $dest, $recursive = true)
 {
     if (false === $this->isConnected()) {
         $this->connectAndLogin();
     }
     $this->dispatcher->dispatch(TransporterEvents::TRANSPORTER_COPY, new TransporterEvent($this, array('dest' => $dest, 'src' => $src)));
     $recursiveFlag = $recursive ? 'r' : '';
     // adjust for symlink
     // we want to copy the contents of the symlink (dereference it), but keep links in subfolders
     $lstat = $this->sftp->lstat($src);
     if ($lstat['type'] === 3) {
         $result = $this->sftp->exec(sprintf("readlink -f %s", escapeshellarg($src)));
         if (false === $result) {
             throw new \RuntimeException('Something went wrong: ' . "\n" . implode("\n", (array) $this->sftp->getErrors()));
         }
         $src = trim($result);
     }
     $success = $this->sftp->exec(sprintf("cp -{$recursiveFlag}f %s %s", escapeshellarg($src), escapeshellarg($dest)));
     if (false === $success) {
         throw new \RuntimeException('Something went wrong: ' . "\n" . implode("\n", (array) $this->sftp->getErrors()));
     }
 }