Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ftp.php
Go to the documentation of this file.
1 <?php
8 
11 
15 class Ftp extends AbstractIo
16 {
17  const ERROR_EMPTY_HOST = 1;
18 
20 
22 
23  const ERROR_INVALID_PATH = 4;
24 
25  const ERROR_INVALID_MODE = 5;
26 
28 
30 
36  protected $_config;
37 
43  protected $_conn;
44 
50  protected $_error;
51 
55  protected $_tmpFilename;
56 
77  public function open(array $args = [])
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  }
143 
149  public function close()
150  {
151  return @ftp_close($this->_conn);
152  }
153 
164  public function mkdir($dir, $mode = 0777, $recursive = true)
165  {
166  return @ftp_mkdir($this->_conn, $dir);
167  }
168 
177  public function rmdir($dir, $recursive = false)
178  {
179  return @ftp_rmdir($this->_conn, $dir);
180  }
181 
187  public function pwd()
188  {
189  return @ftp_pwd($this->_conn);
190  }
191 
199  public function cd($dir)
200  {
201  return @ftp_chdir($this->_conn, $dir);
202  }
203 
211  public function read($filename, $dest = null)
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  }
237 
247  public function write($filename, $src, $mode = null)
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  }
270 
278  public function rm($filename)
279  {
280  return @ftp_delete($this->_conn, $filename);
281  }
282 
291  public function mv($src, $dest)
292  {
293  return @ftp_rename($this->_conn, $src, $dest);
294  }
295 
303  public function chmod($filename, $mode)
304  {
305  return @ftp_chmod($this->_conn, $mode, $filename);
306  }
307 
314  public function ls($grep = null)
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  }
325 
330  protected function _tmpFilename($new = false)
331  {
332  if ($new || !$this->_tmpFilename) {
333  $this->_tmpFilename = tempnam(md5(uniqid(rand(), true)), '');
334  }
335  return $this->_tmpFilename;
336  }
337 }
chmod($filename, $mode)
Definition: Ftp.php:303
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
mkdir($dir, $mode=0777, $recursive=true)
Definition: Ftp.php:164
read($filename, $dest=null)
Definition: Ftp.php:211
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
rmdir($dir, $recursive=false)
Definition: Ftp.php:177
write($filename, $src, $mode=null)
Definition: Ftp.php:247