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

Public Member Functions

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

Data Fields

const ERROR_EMPTY_HOST = 1
 
const ERROR_INVALID_CONNECTION = 2
 
const ERROR_INVALID_LOGIN = 3
 
const ERROR_INVALID_PATH = 4
 
const ERROR_INVALID_MODE = 5
 
const ERROR_INVALID_DESTINATION = 6
 
const ERROR_INVALID_SOURCE = 7
 

Protected Member Functions

 _tmpFilename ($new=false)
 

Protected Attributes

 $_config
 
 $_conn
 
 $_error
 
 $_tmpFilename
 
- Protected Attributes inherited from AbstractIo
 $_allowCreateFolders = false
 

Detailed Description

FTP client

Definition at line 15 of file Ftp.php.

Member Function Documentation

◆ _tmpFilename()

_tmpFilename (   $new = false)
protected
Parameters
bool$new
Returns
string

Definition at line 330 of file Ftp.php.

331  {
332  if ($new || !$this->_tmpFilename) {
333  $this->_tmpFilename = tempnam(md5(uniqid(rand(), true)), '');
334  }
335  return $this->_tmpFilename;
336  }

◆ cd()

cd (   $dir)

Change current working directory

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

Implements IoInterface.

Definition at line 199 of file Ftp.php.

200  {
201  return @ftp_chdir($this->_conn, $dir);
202  }

◆ chmod()

chmod (   $filename,
  $mode 
)

Change mode of a directory or a file

Parameters
string$filename
int$mode
Returns
bool

Implements IoInterface.

Definition at line 303 of file Ftp.php.

304  {
305  return @ftp_chmod($this->_conn, $mode, $filename);
306  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ close()

close ( )

Close a connection

Returns
bool

Implements IoInterface.

Definition at line 149 of file Ftp.php.

150  {
151  return @ftp_close($this->_conn);
152  }

◆ ls()

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

Implements IoInterface.

Definition at line 314 of file Ftp.php.

315  {
316  $ls = @ftp_nlist($this->_conn, '.');
317 
318  $list = [];
319  foreach ($ls as $file) {
320  $list[] = ['text' => $file, 'id' => $this->pwd() . '/' . $file];
321  }
322 
323  return $list;
324  }

◆ mkdir()

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

Create a directory

Todo:
implement $mode and $recursive
Parameters
string$dir
int$mode
bool$recursive
Returns
bool @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements IoInterface.

Definition at line 164 of file Ftp.php.

165  {
166  return @ftp_mkdir($this->_conn, $dir);
167  }

◆ mv()

mv (   $src,
  $dest 
)

Rename or move a directory or a file

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

Implements IoInterface.

Definition at line 291 of file Ftp.php.

292  {
293  return @ftp_rename($this->_conn, $src, $dest);
294  }

◆ open()

open ( array  $args = [])

Open a connection

Possible argument keys:

  • host required
  • port default 21
  • timeout default 90
  • user default anonymous
  • password default empty
  • ssl default false
  • passive default false
  • path default empty
  • file_mode default FTP_BINARY
Parameters
array$args
Returns
true
Exceptions
LocalizedException@SuppressWarnings(PHPMD.CyclomaticComplexity) @SuppressWarnings(PHPMD.NPathComplexity)

Implements IoInterface.

Definition at line 77 of file Ftp.php.

78  {
79  if (empty($args['host'])) {
80  $this->_error = self::ERROR_EMPTY_HOST;
81  throw new LocalizedException(new Phrase('The specified host is empty. Set the host and try again.'));
82  }
83 
84  if (empty($args['port'])) {
85  $args['port'] = 21;
86  }
87 
88  if (empty($args['user'])) {
89  $args['user'] = 'anonymous';
90  $args['password'] = '[email protected]';
91  }
92 
93  if (empty($args['password'])) {
94  $args['password'] = '';
95  }
96 
97  if (empty($args['timeout'])) {
98  $args['timeout'] = 90;
99  }
100 
101  if (empty($args['file_mode'])) {
102  $args['file_mode'] = FTP_BINARY;
103  }
104 
105  $this->_config = $args;
106 
107  if (empty($this->_config['ssl'])) {
108  $this->_conn = @ftp_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
109  } else {
110  $this->_conn = @ftp_ssl_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
111  }
112  if (!$this->_conn) {
113  $this->_error = self::ERROR_INVALID_CONNECTION;
114  throw new LocalizedException(
115  new Phrase("The FTP connection couldn't be established because of an invalid host or port.")
116  );
117  }
118 
119  if (!@ftp_login($this->_conn, $this->_config['user'], $this->_config['password'])) {
120  $this->_error = self::ERROR_INVALID_LOGIN;
121  $this->close();
122  throw new LocalizedException(new Phrase('The username or password is invalid. Verify both and try again.'));
123  }
124 
125  if (!empty($this->_config['path'])) {
126  if (!@ftp_chdir($this->_conn, $this->_config['path'])) {
127  $this->_error = self::ERROR_INVALID_PATH;
128  $this->close();
129  throw new LocalizedException(new Phrase('The path is invalid. Verify and try again.'));
130  }
131  }
132 
133  if (!empty($this->_config['passive'])) {
134  if (!@ftp_pasv($this->_conn, true)) {
135  $this->_error = self::ERROR_INVALID_MODE;
136  $this->close();
137  throw new LocalizedException(new Phrase('The file transfer mode is invalid. Verify and try again.'));
138  }
139  }
140 
141  return true;
142  }

◆ pwd()

pwd ( )

Get current working directory

Returns
string

Implements IoInterface.

Definition at line 187 of file Ftp.php.

188  {
189  return @ftp_pwd($this->_conn);
190  }

◆ read()

read (   $filename,
  $dest = null 
)

Read a file to result, file or stream

Parameters
string$filename
string | resource | null$destdestination file name, stream, or if null will return file contents
Returns
false|string

Implements IoInterface.

Definition at line 211 of file Ftp.php.

212  {
213  if (is_string($dest)) {
214  $result = ftp_get($this->_conn, $dest, $filename, $this->_config['file_mode']);
215  } else {
216  if (is_resource($dest)) {
217  $stream = $dest;
218  } elseif ($dest === null) {
219  $stream = tmpfile();
220  } else {
221  $this->_error = self::ERROR_INVALID_DESTINATION;
222  return false;
223  }
224 
225  $result = ftp_fget($this->_conn, $stream, $filename, $this->_config['file_mode']);
226 
227  if ($dest === null) {
228  fseek($stream, 0);
229  $result = '';
230  for ($result = ''; $s = fread($stream, 4096); $result .= $s) {
231  }
232  fclose($stream);
233  }
234  }
235  return $result;
236  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ rm()

rm (   $filename)

Delete a file

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

Implements IoInterface.

Definition at line 278 of file Ftp.php.

279  {
280  return @ftp_delete($this->_conn, $filename);
281  }

◆ rmdir()

rmdir (   $dir,
  $recursive = false 
)

Delete a directory

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

Implements IoInterface.

Definition at line 177 of file Ftp.php.

178  {
179  return @ftp_rmdir($this->_conn, $dir);
180  }

◆ write()

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

Write a file from string, file or stream

Parameters
string$filename
string | resource$srcfilename, string data or source stream
null$mode
Returns
bool @SuppressWarnings(PHPMD.UnusedFormalParameter)

Implements IoInterface.

Definition at line 247 of file Ftp.php.

248  {
249  if (is_string($src) && is_readable($src)) {
250  return @ftp_put($this->_conn, $filename, $src, $this->_config['file_mode']);
251  } else {
252  if (is_string($src)) {
253  $stream = tmpfile();
254  fputs($stream, $src);
255  fseek($stream, 0);
256  } elseif (is_resource($src)) {
257  $stream = $src;
258  } else {
259  $this->_error = self::ERROR_INVALID_SOURCE;
260  return false;
261  }
262 
263  $result = ftp_fput($this->_conn, $filename, $stream, $this->_config['file_mode']);
264  if (is_string($src)) {
265  fclose($stream);
266  }
267  return $result;
268  }
269  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

Field Documentation

◆ $_config

$_config
protected

Definition at line 36 of file Ftp.php.

◆ $_conn

$_conn
protected

Definition at line 43 of file Ftp.php.

◆ $_error

$_error
protected

Definition at line 50 of file Ftp.php.

◆ $_tmpFilename

$_tmpFilename
protected

Definition at line 55 of file Ftp.php.

◆ ERROR_EMPTY_HOST

const ERROR_EMPTY_HOST = 1

Definition at line 17 of file Ftp.php.

◆ ERROR_INVALID_CONNECTION

const ERROR_INVALID_CONNECTION = 2

Definition at line 19 of file Ftp.php.

◆ ERROR_INVALID_DESTINATION

const ERROR_INVALID_DESTINATION = 6

Definition at line 27 of file Ftp.php.

◆ ERROR_INVALID_LOGIN

const ERROR_INVALID_LOGIN = 3

Definition at line 21 of file Ftp.php.

◆ ERROR_INVALID_MODE

const ERROR_INVALID_MODE = 5

Definition at line 25 of file Ftp.php.

◆ ERROR_INVALID_PATH

const ERROR_INVALID_PATH = 4

Definition at line 23 of file Ftp.php.

◆ ERROR_INVALID_SOURCE

const ERROR_INVALID_SOURCE = 7

Definition at line 29 of file Ftp.php.


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