phpseclib\Net\SFTP::rmdir PHP Method

rmdir() public method

Removes a directory.
public rmdir ( string $dir ) : boolean
$dir string
return boolean
    function rmdir($dir)
    {
        if (!($this->bitmap & SSH2::MASK_LOGIN)) {
            return false;
        }
        $dir = $this->_realpath($dir);
        if ($dir === false) {
            return false;
        }
        if (!$this->_send_sftp_packet(NET_SFTP_RMDIR, pack('Na*', strlen($dir), $dir))) {
            return false;
        }
        $response = $this->_get_sftp_packet();
        if ($this->packet_type != NET_SFTP_STATUS) {
            throw new \UnexpectedValueException('Expected SSH_FXP_STATUS');
        }
        if (strlen($response) < 4) {
            return false;
        }
        extract(unpack('Nstatus', Strings::shift($response, 4)));
        if ($status != NET_SFTP_STATUS_OK) {
            // presumably SSH_FX_NO_SUCH_FILE or SSH_FX_PERMISSION_DENIED?
            $this->_logError($response, $status);
            return false;
        }
        $this->_remove_from_stat_cache($dir);
        // the following will do a soft delete, which would be useful if you deleted a file
        // and then tried to do a stat on the deleted file. the above, in contrast, does
        // a hard delete
        //$this->_update_stat_cache($dir, false);
        return true;
    }

Usage Example

Esempio n. 1
0
 /**
  * sync local directory with ftp directory
  *
  * @param string        $src
  * @param string        $dst
  * @param callable|null $syncop
  *
  * @throws GlSyncFtpException
  */
 public function syncDirectory($src, $dst, callable $syncop = null)
 {
     $this->login();
     $files = [];
     $dirs = [];
     $this->getFiles($dst, "", $files, $dirs);
     // delete on ftp server, files not present in local directory
     foreach ($files as $name => $raw) {
         if (!file_exists($src . $name)) {
             $filepathFtp = $dst . strtr($name, ["\\" => "/"]);
             if ($syncop) {
                 $syncop(self::DELETE_FILE, $filepathFtp);
             }
             $this->sftp->delete($filepathFtp);
         }
     }
     // delete on ftp server, unknowns directories
     $dirs = array_reverse($dirs);
     foreach ($dirs as $name => $raw) {
         if (!file_exists($src . $name)) {
             $filepathFtp = $dst . strtr($name, ["\\" => "/"]);
             if ($syncop) {
                 $syncop(self::DELETE_DIR, $filepathFtp);
             }
             $this->sftp->rmdir($filepathFtp);
         }
     }
     // create new directories
     $finderdir = new Finder();
     $finderdir->directories()->ignoreDotFiles(false)->followLinks()->in($src)->notName('.git*');
     /**
      * @var SplFileInfo $dir
      */
     foreach ($finderdir as $dir) {
         $dirpathFtp = $dst . "/" . strtr($dir->getRelativePathname(), ["\\" => "/"]);
         $stat = $this->sftp->stat($dirpathFtp);
         if (!$stat) {
             if ($syncop) {
                 $syncop(self::CREATE_DIR, $dirpathFtp);
             }
             $this->sftp->mkdir($dirpathFtp, $dir->getRealPath(), SFTP::SOURCE_LOCAL_FILE);
             $this->sftp->chmod(0755, $dirpathFtp, $dir->getRealPath());
         }
     }
     // copy new files or update if younger
     $finderdir = new Finder();
     $finderdir->files()->ignoreDotFiles(false)->followLinks()->in($src)->notName('.git*');
     /**
      * @var SplFileInfo $file
      */
     foreach ($finderdir as $file) {
         $filepathFtp = $dst . "/" . strtr($file->getRelativePathname(), ["\\" => "/"]);
         $stat = $this->sftp->stat($filepathFtp);
         if (!$stat) {
             if ($syncop) {
                 $syncop(self::NEW_FILE, $filepathFtp);
             }
             $this->sftp->put($filepathFtp, $file->getRealPath(), SFTP::SOURCE_LOCAL_FILE);
         } else {
             $size = $this->sftp->size($filepathFtp);
             if ($file->getMTime() > $stat['mtime'] || $file->getSize() != $size) {
                 if ($syncop) {
                     $syncop(self::UPDATE_FILE, $filepathFtp);
                 }
                 $this->sftp->put($filepathFtp, $file->getRealPath(), SFTP::SOURCE_LOCAL_FILE);
             }
         }
     }
 }
All Usage Examples Of phpseclib\Net\SFTP::rmdir