Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
Ftp Class Reference

Public Member Functions

 mdkir ($name)
 
 mkdirRecursive ($path, $mode=0777)
 
 login ($login="anonymous", $password="[email protected]")
 
 validateConnectionString ($string)
 
 connect ($string, $timeout=900)
 
 fput ($remoteFile, $handle, $mode=FTP_BINARY, $startPos=0)
 
 put ($remoteFile, $localFile, $mode=FTP_BINARY, $startPos=0)
 
 getcwd ()
 
 raw ($cmd)
 
 upload ($remote, $local, $dirMode=0777, $ftpMode=FTP_BINARY)
 
 download ($remote, $local, $ftpMode=FTP_BINARY)
 
 pasv ($pasv)
 
 close ()
 
 chmod ($mode, $remoteFile)
 
 chdir ($dir)
 
 cdup ()
 
 get ($localFile, $remoteFile, $fileMode=FTP_BINARY, $resumeOffset=0)
 
 nlist ($dir="/")
 
 rawlist ($dir="/", $recursive=false)
 
 fileExists ($path, $excludeIfIsDir=true)
 
 ls ($dir="/", $recursive=false)
 
 correctFilePath ($str)
 
 delete ($file)
 

Static Public Member Functions

static byteconvert ($bytes)
 
static chmodnum ($chmod)
 

Protected Member Functions

 checkConnected ()
 

Protected Attributes

 $_conn = false
 

Detailed Description

Class to work with remote FTP server

Definition at line 12 of file Ftp.php.

Member Function Documentation

◆ byteconvert()

static byteconvert (   $bytes)
static

Convert byte count to float KB/MB format

Parameters
int$bytes
Returns
string

Definition at line 384 of file Ftp.php.

385  {
386  $symbol = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
387  $exp = floor(log($bytes) / log(1024));
388  return sprintf('%.2f ' . $symbol[$exp], $bytes / pow(1024, floor($exp)));
389  }

◆ cdup()

cdup ( )

ftp_cdup wrapper

Returns
bool

Definition at line 328 of file Ftp.php.

329  {
330  $this->checkConnected();
331  return @ftp_cdup($this->_conn);
332  }

◆ chdir()

chdir (   $dir)

ftp_chdir wrapper

Parameters
string$dir
Returns
bool

Definition at line 317 of file Ftp.php.

318  {
319  $this->checkConnected();
320  return @ftp_chdir($this->_conn, $dir);
321  }

◆ checkConnected()

checkConnected ( )
protected

Check connected, throw exception if not

Exceptions

Definition at line 27 of file Ftp.php.

28  {
29  if (!$this->_conn) {
30  throw new \Exception(__CLASS__ . " - no connection established with server");
31  }
32  }

◆ chmod()

chmod (   $mode,
  $remoteFile 
)

ftp_chmod wrapper

Parameters
int$mode
string$remoteFile
Returns
int The new file permissions on success or FALSE on error.

Definition at line 305 of file Ftp.php.

306  {
307  $this->checkConnected();
308  return @ftp_chmod($this->_conn, $mode, $remoteFile);
309  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ chmodnum()

static chmodnum (   $chmod)
static

Chmod string "-rwxrwxrwx" to "777" converter

Parameters
string$chmod
Returns
string

Definition at line 397 of file Ftp.php.

398  {
399  $trans = ['-' => '0', 'r' => '4', 'w' => '2', 'x' => '1'];
400  $chmod = substr(strtr($chmod, $trans), 1);
401  $array = str_split($chmod, 3);
402  return array_sum(str_split($array[0])) . array_sum(str_split($array[1])) . array_sum(str_split($array[2]));
403  }

◆ close()

close ( )

Close FTP connection

Returns
void

Definition at line 291 of file Ftp.php.

292  {
293  if ($this->_conn) {
294  @ftp_close($this->_conn);
295  }
296  }

◆ connect()

connect (   $string,
  $timeout = 900 
)

Connect to server using connect string Connection string: ftp://user:pass@server:port/path user,pass,port,path are optional parts

Parameters
string$string
int$timeout
Returns
void
Exceptions

Definition at line 127 of file Ftp.php.

128  {
129  $params = $this->validateConnectionString($string);
130  $port = isset($params['port']) ? intval($params['port']) : 21;
131 
132  $this->_conn = ftp_connect($params['host'], $port, $timeout);
133 
134  if (!$this->_conn) {
135  throw new \Exception("Cannot connect to host: {$params['host']}");
136  }
137  if (isset($params['user']) && isset($params['pass'])) {
138  $this->login($params['user'], $params['pass']);
139  } else {
140  $this->login();
141  }
142  if (isset($params['path'])) {
143  if (!$this->chdir($params['path'])) {
144  throw new \Exception("Cannot chdir after login to: {$params['path']}");
145  }
146  }
147  }
validateConnectionString($string)
Definition: Ftp.php:99
login($login="anonymous", $password="[email protected]")
Definition: Ftp.php:82
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18

◆ correctFilePath()

correctFilePath (   $str)

Correct file path

Parameters
string$str
Returns
string

Definition at line 478 of file Ftp.php.

479  {
480  $str = str_replace("\\", "/", $str);
481  $str = preg_replace("/^.\//", "", $str);
482  return $str;
483  }

◆ delete()

delete (   $file)

Delete file

Parameters
string$file
Returns
bool

Definition at line 491 of file Ftp.php.

492  {
493  $this->checkConnected();
494  $file = $this->correctFilePath($file);
495  return @ftp_delete($this->_conn, $file);
496  }

◆ download()

download (   $remote,
  $local,
  $ftpMode = FTP_BINARY 
)

Download remote file to local machine

Parameters
string$remote
string$local
int$ftpModeFTP_BINARY|FTP_ASCII
Returns
bool

Definition at line 268 of file Ftp.php.

269  {
270  $this->checkConnected();
271  return $this->get($local, $remote, $ftpMode);
272  }

◆ fileExists()

fileExists (   $path,
  $excludeIfIsDir = true 
)

Check whether file exists

Parameters
string$path
bool$excludeIfIsDir
Returns
bool

Definition at line 412 of file Ftp.php.

413  {
414  $path = $this->correctFilePath($path);
415  $globalPathMode = substr($path, 0, 1) == "/";
416 
417  $file = basename($path);
418  $dir = $globalPathMode ? dirname($path) : $this->getcwd() . "/" . $path;
419  $data = $this->ls($dir);
420  foreach ($data as $row) {
421  if ($file == $row['name']) {
422  if ($excludeIfIsDir && $row['dir']) {
423  continue;
424  }
425  return true;
426  }
427  }
428  return false;
429  }
ls($dir="/", $recursive=false)
Definition: Ftp.php:439

◆ fput()

fput (   $remoteFile,
  $handle,
  $mode = FTP_BINARY,
  $startPos = 0 
)

ftp_fput wrapper

Parameters
string$remoteFile
resource$handle
int$modeFTP_BINARY | FTP_ASCII
int$startPos
Returns
bool

Definition at line 158 of file Ftp.php.

159  {
160  $this->checkConnected();
161  return @ftp_fput($this->_conn, $remoteFile, $handle, $mode, $startPos);
162  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$handle

◆ get()

get (   $localFile,
  $remoteFile,
  $fileMode = FTP_BINARY,
  $resumeOffset = 0 
)

ftp_get wrapper

Parameters
string$localFile
string$remoteFile
int$fileModeFTP_BINARY | FTP_ASCII
int$resumeOffset
Returns
bool @SuppressWarnings(PHPMD.BooleanGetMethodName)

Definition at line 344 of file Ftp.php.

345  {
346  $remoteFile = $this->correctFilePath($remoteFile);
347  $this->checkConnected();
348  return @ftp_get($this->_conn, $localFile, $remoteFile, $fileMode, $resumeOffset);
349  }

◆ getcwd()

getcwd ( )

Get current working directory

Returns
false|string

Definition at line 184 of file Ftp.php.

185  {
186  $d = $this->raw("pwd");
187  $data = explode(" ", $d[0], 3);
188  if (empty($data[1])) {
189  return false;
190  }
191  if (intval($data[0]) != 257) {
192  return false;
193  }
194  $out = trim($data[1], '"');
195  if ($out !== "/") {
196  $out = rtrim($out, "/");
197  }
198  return $out;
199  }

◆ login()

login (   $login = "anonymous",
  $password = "[email protected]" 
)

Try to login to server

Parameters
string$login
string$password
Returns
bool
Exceptions

Definition at line 82 of file Ftp.php.

83  {
84  $this->checkConnected();
85  $res = @ftp_login($this->_conn, $login, $password);
86  if (!$res) {
87  throw new \Exception("Invalid login credentials");
88  }
89  return $res;
90  }

◆ ls()

ls (   $dir = "/",
  $recursive = false 
)

Get directory contents in PHP array

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

Definition at line 439 of file Ftp.php.

440  {
441  $dir = $this->correctFilePath($dir);
442  $rawfiles = (array)$this->rawlist($dir, $recursive);
443  $structure = [];
444  $arraypointer = & $structure;
445  foreach ($rawfiles as $rawfile) {
446  if ($rawfile[0] == '/') {
447  $paths = array_slice(explode('/', str_replace(':', '', $rawfile)), 1);
448  $arraypointer = & $structure;
449  foreach ($paths as $path) {
450  foreach ($arraypointer as $i => $file) {
451  if ($file['name'] == $path) {
452  $arraypointer = & $arraypointer[$i]['children'];
453  break;
454  }
455  }
456  }
457  } elseif (!empty($rawfile)) {
458  $info = preg_split("/[\s]+/", $rawfile, 9);
459  $arraypointer[] = [
460  'name' => $info[8],
461  'dir' => $info[0][0] == 'd',
462  'size' => (int)$info[4],
463  'chmod' => self::chmodnum($info[0]),
464  'rawdata' => $info,
465  'raw' => $rawfile,
466  ];
467  }
468  }
469  return $structure;
470  }
rawlist($dir="/", $recursive=false)
Definition: Ftp.php:371
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$paths
Definition: _bootstrap.php:83
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$i
Definition: gallery.phtml:31

◆ mdkir()

mdkir (   $name)

ftp_mkdir wrapper

Parameters
string$name
Returns
string the newly created directory name on success or FALSE on error.

Definition at line 40 of file Ftp.php.

41  {
42  $this->checkConnected();
43  return @ftp_mkdir($this->_conn, $name);
44  }
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ mkdirRecursive()

mkdirRecursive (   $path,
  $mode = 0777 
)

Make dir recursive

Parameters
string$path
int$mode
Returns
bool

Definition at line 53 of file Ftp.php.

54  {
55  $this->checkConnected();
56  $dir = explode("/", $path);
57  $path = "";
58  $ret = true;
59  for ($i = 0; $i < count($dir); $i++) {
60  $path .= "/" . $dir[$i];
61  if (!@ftp_chdir($this->_conn, $path)) {
62  @ftp_chdir($this->_conn, "/");
63  if (!@ftp_mkdir($this->_conn, $path)) {
64  $ret = false;
65  break;
66  } else {
67  @ftp_chmod($this->_conn, $mode, $path);
68  }
69  }
70  }
71  return $ret;
72  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
$i
Definition: gallery.phtml:31

◆ nlist()

nlist (   $dir = "/")

ftp_nlist wrapper

Parameters
string$dir
Returns
bool

Definition at line 357 of file Ftp.php.

358  {
359  $this->checkConnected();
360  $dir = $this->correctFilePath($dir);
361  return @ftp_nlist($this->_conn, $dir);
362  }

◆ pasv()

pasv (   $pasv)

ftp_pasv wrapper

Parameters
bool$pasv
Returns
bool

Definition at line 280 of file Ftp.php.

281  {
282  $this->checkConnected();
283  return @ftp_pasv($this->_conn, (bool)$pasv);
284  }

◆ put()

put (   $remoteFile,
  $localFile,
  $mode = FTP_BINARY,
  $startPos = 0 
)

ftp_put wrapper

Parameters
string$remoteFile
string$localFile
int$modeFTP_BINARY | FTP_ASCII
int$startPos
Returns
bool

Definition at line 173 of file Ftp.php.

174  {
175  $this->checkConnected();
176  return ftp_put($this->_conn, $remoteFile, $localFile, $mode, $startPos);
177  }
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15

◆ raw()

raw (   $cmd)

ftp_raw wrapper

Parameters
string$cmd
Returns
array The server's response as an array of strings.

Definition at line 207 of file Ftp.php.

208  {
209  $this->checkConnected();
210  return @ftp_raw($this->_conn, $cmd);
211  }

◆ rawlist()

rawlist (   $dir = "/",
  $recursive = false 
)

ftp_rawlist wrapper

Parameters
string$dir
bool$recursive
Returns
array an array where each element corresponds to one line of text.

Definition at line 371 of file Ftp.php.

372  {
373  $this->checkConnected();
374  $dir = $this->correctFilePath($dir);
375  return @ftp_rawlist($this->_conn, $dir, $recursive);
376  }

◆ upload()

upload (   $remote,
  $local,
  $dirMode = 0777,
  $ftpMode = FTP_BINARY 
)

Upload local file to remote

Can be used for relative and absoulte remote paths Relative: use chdir before calling this

Parameters
string$remote
string$local
int$dirMode
int$ftpMode
Returns
bool
Exceptions

Definition at line 226 of file Ftp.php.

227  {
228  $this->checkConnected();
229 
230  if (!file_exists($local)) {
231  throw new \Exception("Local file doesn't exist: {$local}");
232  }
233  if (!is_readable($local)) {
234  throw new \Exception("Local file is not readable: {$local}");
235  }
236  if (is_dir($local)) {
237  throw new \Exception("Directory given instead of file: {$local}");
238  }
239 
240  $globalPathMode = substr($remote, 0, 1) == "/";
241  $dirname = dirname($remote);
242  $cwd = $this->getcwd();
243  if (false === $cwd) {
244  throw new \Exception("Server returns something awful on PWD command");
245  }
246 
247  if (!$globalPathMode) {
248  $dirname = $cwd . "/" . $dirname;
249  $remote = $cwd . "/" . $remote;
250  }
251  $res = $this->mkdirRecursive($dirname, $dirMode);
252  $this->chdir($cwd);
253 
254  if (!$res) {
255  return false;
256  }
257  return $this->put($remote, $local, $ftpMode);
258  }
mkdirRecursive($path, $mode=0777)
Definition: Ftp.php:53
put($remoteFile, $localFile, $mode=FTP_BINARY, $startPos=0)
Definition: Ftp.php:173

◆ validateConnectionString()

validateConnectionString (   $string)

Validate connection string

Parameters
string$string
Exceptions

Definition at line 99 of file Ftp.php.

100  {
101  $data = parse_url($string);
102  if (false === $data) {
103  throw new \Exception("Connection string invalid: '{$string}'");
104  }
105  if ($data['scheme'] != 'ftp') {
106  throw new \Exception("Support for scheme unsupported: '{$data['scheme']}'");
107  }
108 
109  // Decode user & password strings from URL
110  foreach (array_intersect(array_keys($data), ['user','pass']) as $key) {
111  $data[$key] = urldecode($data[$key]);
112  }
113 
114  return $data;
115  }

Field Documentation

◆ $_conn

$_conn = false
protected

Definition at line 19 of file Ftp.php.


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