Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields | Protected Attributes
Sftp Class Reference
Inheritance diagram for Sftp:
AbstractIo IoInterface

Public Member Functions

 open (array $args=[])
 
 close ()
 
 mkdir ($dir, $mode=0777, $recursive=true)
 
 rmdir ($dir, $recursive=false)
 
 pwd ()
 
 cd ($dir)
 
 read ($filename, $destination=null)
 
 write ($filename, $source, $mode=null)
 
 rm ($filename)
 
 mv ($source, $destination)
 
 chmod ($filename, $mode)
 
 ls ($grep=null)
 
 rawls ()
 
- Public Member Functions inherited from AbstractIo
 setAllowCreateFolders ($flag)
 
 open (array $args=[])
 
 dirsep ()
 
 getCleanPath ($path)
 
 allowedPath ($haystackPath, $needlePath)
 

Data Fields

const REMOTE_TIMEOUT = 10
 
const SSH2_PORT = 22
 

Protected Attributes

 $_connection = null
 
- Protected Attributes inherited from AbstractIo
 $_allowCreateFolders = false
 

Detailed Description

Sftp client interface

http://www.php.net/manual/en/function.ssh2-connect.php

Definition at line 14 of file Sftp.php.

Member Function Documentation

◆ cd()

cd (   $dir)

Change current working directory

Parameters
string$dir
Returns
bool @SuppressWarnings(PHPMD.ShortMethodName)

Implements IoInterface.

Definition at line 156 of file Sftp.php.

157  {
158  return $this->_connection->chdir($dir);
159  }

◆ chmod()

chmod (   $filename,
  $mode 
)

Change mode of a directory or a file

Parameters
string$filename
int$mode
Returns
mixed

Implements IoInterface.

Definition at line 222 of file Sftp.php.

223  {
224  return $this->_connection->chmod($mode, $filename);
225  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ close()

close ( )

Close a connection

Returns
void

Implements IoInterface.

Definition at line 59 of file Sftp.php.

60  {
61  $this->_connection->disconnect();
62  }

◆ ls()

ls (   $grep = null)

Get list of cwd subdirectories and files

Parameters
null$grepignored parameter
Returns
array @SuppressWarnings(PHPMD.ShortMethodName) @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements IoInterface.

Definition at line 235 of file Sftp.php.

236  {
237  $list = $this->_connection->nlist();
238  $currentWorkingDir = $this->pwd();
239  $result = [];
240  foreach ($list as $name) {
241  $result[] = ['text' => $name, 'id' => "{$currentWorkingDir}/{$name}"];
242  }
243  return $result;
244  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ mkdir()

mkdir (   $dir,
  $mode = 0777,
  $recursive = true 
)

Create a directory

Parameters
string$dir
int$modeignored here; uses logged-in user's umask
bool$recursiveanalogous to mkdir -p

Note: if $recursive is true and an error occurs mid-execution, false is returned and some part of the hierarchy might be created. No rollback is performed.

Returns
bool @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements IoInterface.

Definition at line 78 of file Sftp.php.

79  {
80  if ($recursive) {
81  $no_errors = true;
82  $dirList = explode('/', $dir);
83  reset($dirList);
84  $currentWorkingDir = $this->_connection->pwd();
85  while ($no_errors && ($dir_item = next($dirList))) {
86  $no_errors = $this->_connection->mkdir($dir_item) && $this->_connection->chdir($dir_item);
87  }
88  $this->_connection->chdir($currentWorkingDir);
89  return $no_errors;
90  } else {
91  return $this->_connection->mkdir($dir);
92  }
93  }
if(!file_exists($installConfigFile)) $dirList
Definition: bootstrap.php:57

◆ mv()

mv (   $source,
  $destination 
)

Rename or move a directory or a file

Parameters
string$source
string$destination
Returns
bool @SuppressWarnings(PHPMD.ShortMethodName)

Implements IoInterface.

Definition at line 210 of file Sftp.php.

211  {
212  return $this->_connection->rename($source, $destination);
213  }
$source
Definition: source.php:23

◆ open()

open ( array  $args = [])

Open a SFTP connection to a remote site.

Parameters
array$argsConnection arguments string $args[host] Remote hostname string $args[username] Remote username string $args[password] Connection password int $args[timeout] Connection timeout [=10]
Returns
void
Exceptions

Implements IoInterface.

Definition at line 35 of file Sftp.php.

36  {
37  if (!isset($args['timeout'])) {
38  $args['timeout'] = self::REMOTE_TIMEOUT;
39  }
40  if (strpos($args['host'], ':') !== false) {
41  list($host, $port) = explode(':', $args['host'], 2);
42  } else {
43  $host = $args['host'];
44  $port = self::SSH2_PORT;
45  }
46  $this->_connection = new \phpseclib\Net\SFTP($host, $port, $args['timeout']);
47  if (!$this->_connection->login($args['username'], $args['password'])) {
48  throw new \Exception(
49  sprintf("Unable to open SFTP connection as %s@%s", $args['username'], $args['host'])
50  );
51  }
52  }

◆ pwd()

pwd ( )

Get current working directory

Returns
mixed

Implements IoInterface.

Definition at line 144 of file Sftp.php.

145  {
146  return $this->_connection->pwd();
147  }

◆ rawls()

rawls ( )

Returns a list of files in the current directory

Returns
mixed

Definition at line 251 of file Sftp.php.

252  {
253  $list = $this->_connection->rawlist();
254  return $list;
255  }

◆ read()

read (   $filename,
  $destination = null 
)

Read a file

Parameters
string$filenameremote file name
string | null$destinationlocal file name (optional)
Returns
mixed

Implements IoInterface.

Definition at line 168 of file Sftp.php.

169  {
170  if ($destination === null) {
171  $destination = false;
172  }
173  return $this->_connection->get($filename, $destination);
174  }

◆ rm()

rm (   $filename)

Delete a file

Parameters
string$filename
Returns
bool @SuppressWarnings(PHPMD.ShortMethodName)

Implements IoInterface.

Definition at line 197 of file Sftp.php.

198  {
199  return $this->_connection->delete($filename);
200  }

◆ rmdir()

rmdir (   $dir,
  $recursive = false 
)

Delete a directory

Parameters
string$dir
bool$recursive
Returns
bool
Exceptions

Implements IoInterface.

Definition at line 104 of file Sftp.php.

105  {
106  if ($recursive) {
107  $no_errors = true;
108  $currentWorkingDir = $this->pwd();
109  if (!$this->_connection->chdir($dir)) {
110  throw new \Exception("chdir(): {$dir}: Not a directory");
111  }
112  $list = $this->_connection->nlist();
113  if (!count($list)) {
114  // Go back
115  $this->_connection->chdir($currentWorkingDir);
116  return $this->rmdir($dir, false);
117  } else {
118  foreach ($list as $filename) {
119  if ($this->_connection->chdir($filename)) {
120  // This is a directory
121  $this->_connection->chdir('..');
122  $no_errors = $no_errors && $this->rmdir($filename, $recursive);
123  } else {
124  $no_errors = $no_errors && $this->rm($filename);
125  }
126  }
127  }
128  $no_errors = $no_errors && ($this->_connection->chdir(
129  $currentWorkingDir
130  ) && $this->_connection->rmdir(
131  $dir
132  ));
133  return $no_errors;
134  } else {
135  return $this->_connection->rmdir($dir);
136  }
137  }
rmdir($dir, $recursive=false)
Definition: Sftp.php:104

◆ write()

write (   $filename,
  $source,
  $mode = null 
)

Write a file

Parameters
string$filename
string$sourcestring data or local file name
int$modeignored parameter
Returns
bool

Implements IoInterface.

Definition at line 184 of file Sftp.php.

185  {
186  $mode = is_readable($source) ? \phpseclib\Net\SFTP::SOURCE_LOCAL_FILE : \phpseclib\Net\SFTP::SOURCE_STRING;
187  return $this->_connection->put($filename, $source, $mode);
188  }
$source
Definition: source.php:23
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

Field Documentation

◆ $_connection

phpseclib Net SFTP $_connection = null
protected

Definition at line 22 of file Sftp.php.

◆ REMOTE_TIMEOUT

const REMOTE_TIMEOUT = 10

Definition at line 16 of file Sftp.php.

◆ SSH2_PORT

const SSH2_PORT = 22

Definition at line 17 of file Sftp.php.


The documentation for this class was generated from the following file: