Varien_Io_Ftp Class Reference

Inheritance diagram for Varien_Io_Ftp:

Varien_Io_Abstract Varien_Io_Interface

List of all members.

Public Member Functions

 open (array $args=array())
 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 Attributes

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


Detailed Description

Definition at line 35 of file Ftp.php.


Member Function Documentation

_tmpFilename ( new = false  )  [protected]

Definition at line 320 of file Ftp.php.

00321     {
00322         if ($new || !$this->_tmpFilename) {
00323             $this->_tmpFilename = tempnam( md5(uniqid(rand(), TRUE)), '' );
00324         }
00325         return $this->_tmpFilename;
00326     }

cd ( dir  ) 

Change current working directory

Parameters:
string $dir
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 201 of file Ftp.php.

00202     {
00203         return @ftp_chdir($this->_conn, $dir);
00204     }

chmod ( filename,
mode 
)

Change mode of a directory or a file

Parameters:
string $filename
int $mode
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 300 of file Ftp.php.

00301     {
00302         return @ftp_chmod($this->_conn, $mode, $filename);
00303     }

close (  ) 

Close a connection

Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 155 of file Ftp.php.

00156     {
00157         return @ftp_close($this->_conn);
00158     }

ls ( grep = null  ) 

Get list of cwd subdirectories and files

Implements Varien_Io_Interface.

Definition at line 305 of file Ftp.php.

00306     {
00307         $ls = @ftp_nlist($this->_conn, '.');
00308 
00309         $list = array();
00310         foreach ($ls as $file) {
00311             $list[] = array(
00312                 'text'=>$file,
00313                 'id'=>$this->pwd().'/'.$file,
00314             );
00315         }
00316 
00317         return $list;
00318     }

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

Create a directory

Todo:
implement $mode and $recursive
Parameters:
string $dir
int $mode
boolean $recursive
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 169 of file Ftp.php.

00170     {
00171         return @ftp_mkdir($this->_conn, $dir);
00172     }

mv ( src,
dest 
)

Rename or move a directory or a file

Parameters:
string $src
string $dest
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 288 of file Ftp.php.

00289     {
00290         return @ftp_rename($this->_conn, $src, $dest);
00291     }

open ( array args = array()  ) 

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:
boolean

Reimplemented from Varien_Io_Abstract.

Definition at line 85 of file Ftp.php.

00086     {
00087         if (empty($args['host'])) {
00088             $this->_error = self::ERROR_EMPTY_HOST;
00089             throw new Varien_Io_Exception('Empty host specified');
00090         }
00091 
00092         if (empty($args['port'])) {
00093             $args['port'] = 21;
00094         }
00095 
00096         if (empty($args['user'])) {
00097             $args['user'] = 'anonymous';
00098             $args['password'] = 'anonymous@noserver.com';
00099         }
00100 
00101         if (empty($args['password'])) {
00102             $args['password'] = '';
00103         }
00104 
00105         if (empty($args['timeout'])) {
00106             $args['timeout'] = 90;
00107         }
00108 
00109         if (empty($args['file_mode'])) {
00110             $args['file_mode'] = FTP_BINARY;
00111         }
00112 
00113         $this->_config = $args;
00114 
00115         if (empty($this->_config['ssl'])) {
00116             $this->_conn = @ftp_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
00117         } else {
00118             $this->_conn = @ftp_ssl_connect($this->_config['host'], $this->_config['port'], $this->_config['timeout']);
00119         }
00120         if (!$this->_conn) {
00121             $this->_error = self::ERROR_INVALID_CONNECTION;
00122             throw new Varien_Io_Exception('Could not establish FTP connection, invalid host or port');
00123         }
00124 
00125         if (!@ftp_login($this->_conn, $this->_config['user'], $this->_config['password'])) {
00126             $this->_error = self::ERROR_INVALID_LOGIN;
00127             $this->close();
00128             throw new Varien_Io_Exception('Invalid user name or password');
00129         }
00130 
00131         if (!empty($this->_config['path'])) {
00132             if (!@ftp_chdir($this->_conn, $this->_config['path'])) {
00133                 $this->_error = self::ERROR_INVALID_PATH;
00134                 $this->close();
00135                 throw new Varien_Io_Exception('Invalid path');
00136             }
00137         }
00138 
00139         if (!empty($this->_config['passive'])) {
00140             if (!@ftp_pasv($this->_conn, true)) {
00141                 $this->_error = self::ERROR_INVALID_MODE;
00142                 $this->close();
00143                 throw new Varien_Io_Exception('Invalid file transfer mode');
00144             }
00145         }
00146 
00147         return true;
00148     }

pwd (  ) 

Get current working directory

Returns:
string

Implements Varien_Io_Interface.

Definition at line 190 of file Ftp.php.

00191     {
00192         return @ftp_pwd($this->_conn);
00193     }

read ( filename,
dest = null 
)

Read a file to result, file or stream

Parameters:
string $filename
string|resource|null $dest destination file name, stream, or if null will return file contents
Returns:
string

Implements Varien_Io_Interface.

Definition at line 213 of file Ftp.php.

00214     {
00215         if (is_string($dest)) {
00216             $result = ftp_get($this->_conn, $dest, $filename, $this->_config['file_mode']);
00217         } else {
00218             if (is_resource($dest)) {
00219                 $stream = $dest;
00220             } elseif (is_null($dest)) {
00221                 $stream = tmpfile();
00222             } else {
00223                 $this->_error = self::ERROR_INVALID_DESTINATION;
00224                 return false;
00225             }
00226 
00227             $result = ftp_fget($this->_conn, $stream, $filename, $this->_config['file_mode']);
00228 
00229             if (is_null($dest)) {
00230                 fseek($stream, 0);
00231                 $result = '';
00232                 for ($result = ''; $s = fread($stream, 4096); $result .= $s);
00233                 fclose($stream);
00234             }
00235         }
00236         return $result;
00237     }

rm ( filename  ) 

Delete a file

Parameters:
string $filename
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 276 of file Ftp.php.

00277     {
00278         return @ftp_delete($this->_conn, $filename);
00279     }

rmdir ( dir,
recursive = false 
)

Delete a directory

Parameters:
string $dir
Returns:
boolean

Implements Varien_Io_Interface.

Definition at line 180 of file Ftp.php.

00181     {
00182         return @ftp_rmdir($this->_conn, $dir);
00183     }

write ( filename,
src,
mode = null 
)

Write a file from string, file or stream

Parameters:
string $filename
string|resource $src filename, string data or source stream
Returns:
int|boolean

Implements Varien_Io_Interface.

Definition at line 246 of file Ftp.php.

00247     {
00248         if (is_string($src) && is_readable($src)) {
00249             return @ftp_put($this->_conn, $filename, $src, $this->_config['file_mode']);
00250         } else {
00251             if (is_string($src)) {
00252                 $stream = tmpfile();
00253                 fputs($stream, $src);
00254                 fseek($stream, 0);
00255             } elseif (is_resource($src)) {
00256                 $stream = $src;
00257             } else {
00258                 $this->_error = self::ERROR_INVALID_SOURCE;
00259                 return false;
00260             }
00261 
00262             $result = ftp_fput($this->_conn, $filename, $stream, $this->_config['file_mode']);
00263             if (is_string($src)) {
00264                 fclose($stream);
00265             }
00266             return $result;
00267         }
00268     }


Member Data Documentation

$_config [protected]

Definition at line 50 of file Ftp.php.

$_conn [protected]

Definition at line 57 of file Ftp.php.

$_error [protected]

Definition at line 64 of file Ftp.php.

$_tmpFilename [protected]

Definition at line 66 of file Ftp.php.

const ERROR_EMPTY_HOST = 1

Definition at line 37 of file Ftp.php.

Definition at line 38 of file Ftp.php.

Definition at line 42 of file Ftp.php.

Definition at line 39 of file Ftp.php.

const ERROR_INVALID_MODE = 5

Definition at line 41 of file Ftp.php.

const ERROR_INVALID_PATH = 4

Definition at line 40 of file Ftp.php.

Definition at line 43 of file Ftp.php.


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

Generated on Sat Jul 4 17:25:03 2009 for Magento by  doxygen 1.5.8