CI_FTP Class Reference

List of all members.


Public Member Functions

 CI_FTP ($config=array())
 Constructor - Sets Preferences.
 initialize ($config=array())
 Initialize preferences.
 connect ($config=array())
 FTP Connect.
 _login ()
 FTP Login.
 _is_conn ()
 Validates the connection ID.
 changedir ($path= '', $supress_debug=FALSE)
 Change direcotry.
 mkdir ($path= '', $permissions=NULL)
 Create a directory.
 upload ($locpath, $rempath, $mode= 'auto', $permissions=NULL)
 Upload a file to the server.
 rename ($old_file, $new_file, $move=FALSE)
 Rename (or move) a file.
 move ($old_file, $new_file)
 Move a file.
 delete_file ($filepath)
 Rename (or move) a file.
 delete_dir ($filepath)
 Delete a folder and recursively delete everything (including sub-folders) containted within it.
 chmod ($path, $perm)
 Set file permissions.
 list_files ($path= '.')
 FTP List files in the specified directory.
 mirror ($locpath, $rempath)
 Read a directory and recreate it remotely.
 _getext ($filename)
 Extract the file extension.
 _settype ($ext)
 Set the upload type.
 close ()
 Close the connection.
 _error ($line)
 Display error message.

Public Attributes

 $hostname = ''
 $username = ''
 $password = ''
 $port = 21
 $passive = TRUE
 $debug = FALSE
 $conn_id = FALSE

Detailed Description

Definition at line 27 of file Ftp.php.


Member Function Documentation

CI_FTP::_error ( line  ) 

Display error message.

private

Parameters:
string 
Returns:
bool

Definition at line 606 of file Ftp.php.

References $CI, get_instance(), and show_error().

Referenced by _is_conn(), changedir(), chmod(), connect(), delete_dir(), delete_file(), mkdir(), rename(), and upload().

00607         {
00608                 $CI =& get_instance();
00609                 $CI->lang->load('ftp');
00610                 show_error($CI->lang->line($line));             
00611         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_FTP::_getext ( filename  ) 

Extract the file extension.

private

Parameters:
string 
Returns:
string

Definition at line 534 of file Ftp.php.

Referenced by mirror(), and upload().

00535         {
00536                 if (FALSE === strpos($filename, '.'))
00537                 {
00538                         return 'txt';
00539                 }
00540         
00541                 $x = explode('.', $filename);
00542                 return end($x);
00543         }       

Here is the caller graph for this function:

CI_FTP::_is_conn (  ) 

Validates the connection ID.

private

Returns:
bool

Definition at line 140 of file Ftp.php.

References _error().

Referenced by changedir(), chmod(), close(), delete_dir(), delete_file(), list_files(), mirror(), mkdir(), rename(), and upload().

00141         {
00142                 if ( ! is_resource($this->conn_id))
00143                 {
00144                         if ($this->debug == TRUE)
00145                         {
00146                                 $this->_error('ftp_no_connection');
00147                         }               
00148                         return FALSE;
00149                 }
00150                 return TRUE;
00151         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_FTP::_login (  ) 

FTP Login.

private

Returns:
bool

Definition at line 127 of file Ftp.php.

Referenced by connect().

00128         {
00129                 return @ftp_login($this->conn_id, $this->username, $this->password);
00130         }

Here is the caller graph for this function:

CI_FTP::_settype ( ext  ) 

Set the upload type.

private

Parameters:
string 
Returns:
string

Definition at line 555 of file Ftp.php.

Referenced by mirror(), and upload().

00556         {
00557                 $text_types = array(
00558                                                         'txt',
00559                                                         'text',
00560                                                         'php',
00561                                                         'phps',
00562                                                         'php4',
00563                                                         'js',
00564                                                         'css',
00565                                                         'htm',
00566                                                         'html',
00567                                                         'phtml',
00568                                                         'shtml',
00569                                                         'log',
00570                                                         'xml'
00571                                                         );
00572         
00573         
00574                 return (in_array($ext, $text_types)) ? 'ascii' : 'binary';
00575         }

Here is the caller graph for this function:

CI_FTP::changedir ( path = '',
supress_debug = FALSE 
)

Change direcotry.

The second parameter lets us momentarily turn off debugging so that this function can be used to test for the existance of a folder without throwing an error. There's no FTP equivalent to is_dir() so we do it by trying to change to a particular directory. Internally, this paramter is only used by the "mirror" function below.

public

Parameters:
string 
bool 
Returns:
bool

Definition at line 170 of file Ftp.php.

References _error(), and _is_conn().

Referenced by mirror().

00171         {
00172                 if ($path == '' OR ! $this->_is_conn())
00173                 {
00174                         return FALSE;
00175                 }
00176                 
00177                 $result = @ftp_chdir($this->conn_id, $path);
00178                 
00179                 if ($result === FALSE)
00180                 {
00181                         if ($this->debug == TRUE AND $supress_debug == FALSE)
00182                         {
00183                                 $this->_error('ftp_unable_to_changedir');
00184                         }               
00185                         return FALSE;           
00186                 }
00187                 
00188                 return TRUE;
00189         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_FTP::chmod ( path,
perm 
)

Set file permissions.

public

Parameters:
string the file path
string the permissions
Returns:
bool

Definition at line 419 of file Ftp.php.

References _error(), and _is_conn().

Referenced by mkdir(), and upload().

00420         {
00421                 if ( ! $this->_is_conn())
00422                 {
00423                         return FALSE;
00424                 }
00425 
00426                 // Permissions can only be set when running PHP 5
00427                 if ( ! function_exists('ftp_chmod'))
00428                 {
00429                         if ($this->debug == TRUE)
00430                         {
00431                                 $this->_error('ftp_unable_to_chmod');
00432                         }               
00433                         return FALSE;           
00434                 }
00435         
00436                 $result = @ftp_chmod($this->conn_id, $perm, $path);
00437                 
00438                 if ($result === FALSE)
00439                 {
00440                         if ($this->debug == TRUE)
00441                         {
00442                                 $this->_error('ftp_unable_to_chmod');
00443                         }               
00444                         return FALSE;           
00445                 }
00446                 
00447                 return TRUE;
00448         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_FTP::CI_FTP ( config = array()  ) 

Constructor - Sets Preferences.

The constructor can be passed an array of config values

Definition at line 43 of file Ftp.php.

References $config, initialize(), and log_message().

00044         {               
00045                 if (count($config) > 0)
00046                 {
00047                         $this->initialize($config);
00048                 }       
00049 
00050                 log_message('debug', "FTP Class Initialized");
00051         }

Here is the call graph for this function:

CI_FTP::close (  ) 

Close the connection.

public

Parameters:
string path to source
string path to destination
Returns:
bool

Definition at line 587 of file Ftp.php.

References _is_conn().

00588         {
00589                 if ( ! $this->_is_conn())
00590                 {
00591                         return FALSE;
00592                 }
00593 
00594                 @ftp_close($this->conn_id);
00595         }

Here is the call graph for this function:

CI_FTP::connect ( config = array()  ) 

FTP Connect.

public

Parameters:
array the connection values
Returns:
bool

Definition at line 85 of file Ftp.php.

References $config, _error(), _login(), and initialize().

00086         {               
00087                 if (count($config) > 0)
00088                 {
00089                         $this->initialize($config);
00090                 }       
00091         
00092                 if (FALSE === ($this->conn_id = @ftp_connect($this->hostname, $this->port)))
00093                 {
00094                         if ($this->debug == TRUE)
00095                         {
00096                                 $this->_error('ftp_unable_to_connect');
00097                         }               
00098                         return FALSE;
00099                 }
00100                 
00101                 if ( ! $this->_login())
00102                 {
00103                         if ($this->debug == TRUE)
00104                         {
00105                                 $this->_error('ftp_unable_to_login');
00106                         }               
00107                         return FALSE;
00108                 }
00109                 
00110                 // Set passive mode if needed
00111                 if ($this->passive == TRUE)
00112                 {
00113                         ftp_pasv($this->conn_id, TRUE);
00114                 }
00115                 
00116                 return TRUE;
00117         }

Here is the call graph for this function:

CI_FTP::delete_dir ( filepath  ) 

Delete a folder and recursively delete everything (including sub-folders) containted within it.

public

Parameters:
string 
Returns:
bool

Definition at line 370 of file Ftp.php.

References _error(), _is_conn(), and list_files().

00371         {
00372                 if ( ! $this->_is_conn())
00373                 {
00374                         return FALSE;
00375                 }
00376 
00377                 // Add a trailing slash to the file path if needed
00378                 $filepath = preg_replace("/(.+?)\/*$/", "\\1/",  $filepath);
00379                 
00380                 $list = $this->list_files($filepath);
00381                 
00382                 if ($list !== FALSE AND count($list) > 0)
00383                 {
00384                         foreach ($list as $item)
00385                         {                       
00386                                 // If we can't delete the item it's probaly a folder so
00387                                 // we'll recursively call delete_dir()
00388                                 if ( ! @ftp_delete($this->conn_id, $item))
00389                                 {
00390                                         $this->delete_dir($item);
00391                                 }
00392                         }
00393                 }
00394         
00395                 $result = @ftp_rmdir($this->conn_id, $filepath);
00396                 
00397                 if ($result === FALSE)
00398                 {
00399                         if ($this->debug == TRUE)
00400                         {                               
00401                                 $this->_error('ftp_unable_to_delete');
00402                         }               
00403                         return FALSE;           
00404                 }
00405                 
00406                 return TRUE;
00407         }

Here is the call graph for this function:

CI_FTP::delete_file ( filepath  ) 

Rename (or move) a file.

public

Parameters:
string 
Returns:
bool

Definition at line 339 of file Ftp.php.

References _error(), and _is_conn().

00340         {
00341                 if ( ! $this->_is_conn())
00342                 {
00343                         return FALSE;
00344                 }
00345 
00346                 $result = @ftp_delete($this->conn_id, $filepath);
00347                 
00348                 if ($result === FALSE)
00349                 {
00350                         if ($this->debug == TRUE)
00351                         {                               
00352                                 $this->_error('ftp_unable_to_delete');
00353                         }               
00354                         return FALSE;           
00355                 }
00356                 
00357                 return TRUE;
00358         }

Here is the call graph for this function:

CI_FTP::initialize ( config = array()  ) 

Initialize preferences.

public

Parameters:
array 
Returns:
void

Definition at line 62 of file Ftp.php.

References $config.

Referenced by CI_FTP(), and connect().

00063         {
00064                 foreach ($config as $key => $val)
00065                 {
00066                         if (isset($this->$key))
00067                         {
00068                                 $this->$key = $val;
00069                         }
00070                 }
00071                 
00072                 // Prep the hostname
00073                 $this->hostname = preg_replace('|.+?://|', '', $this->hostname);
00074         }

Here is the caller graph for this function:

CI_FTP::list_files ( path = '.'  ) 

FTP List files in the specified directory.

public

Returns:
array

Definition at line 458 of file Ftp.php.

References _is_conn().

Referenced by delete_dir().

00459         {
00460                 if ( ! $this->_is_conn())
00461                 {
00462                         return FALSE;
00463                 }
00464 
00465                 return ftp_nlist($this->conn_id, $path);
00466         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_FTP::mirror ( locpath,
rempath 
)

Read a directory and recreate it remotely.

This function recursively reads a folder and everything it contains (including sub-folders) and creates a mirror via FTP based on it. Whatever the directory structure of the original file path will be recreated on the server.

public

Parameters:
string path to source with trailing slash
string path to destination - include the base folder with trailing slash
Returns:
bool

Definition at line 482 of file Ftp.php.

References _getext(), _is_conn(), _settype(), changedir(), mkdir(), and upload().

00483         {
00484                 if ( ! $this->_is_conn())
00485                 {
00486                         return FALSE;
00487                 }
00488 
00489                 // Open the local file path
00490                 if ($fp = @opendir($locpath))
00491                 {
00492                         // Attempt to open the remote file path.
00493                         if ( ! $this->changedir($rempath, TRUE))
00494                         {
00495                                 // If it doesn't exist we'll attempt to create the direcotory
00496                                 if ( ! $this->mkdir($rempath) OR ! $this->changedir($rempath))
00497                                 {
00498                                         return FALSE;
00499                                 }
00500                         }
00501                 
00502                         // Recursively read the local directory
00503                         while (FALSE !== ($file = readdir($fp)))
00504                         {
00505                                 if (@is_dir($locpath.$file) && substr($file, 0, 1) != '.')
00506                                 {                                       
00507                                         $this->mirror($locpath.$file."/", $rempath.$file."/");
00508                                 }
00509                                 elseif (substr($file, 0, 1) != ".")
00510                                 {
00511                                         // Get the file extension so we can se the upload type
00512                                         $ext = $this->_getext($file);
00513                                         $mode = $this->_settype($ext);
00514                                         
00515                                         $this->upload($locpath.$file, $rempath.$file, $mode);
00516                                 }
00517                         }
00518                         return TRUE;
00519                 }
00520                 
00521                 return FALSE;
00522         }

Here is the call graph for this function:

CI_FTP::mkdir ( path = '',
permissions = NULL 
)

Create a directory.

public

Parameters:
string 
Returns:
bool

Definition at line 200 of file Ftp.php.

References _error(), _is_conn(), and chmod().

Referenced by mirror().

00201         {
00202                 if ($path == '' OR ! $this->_is_conn())
00203                 {
00204                         return FALSE;
00205                 }
00206         
00207                 $result = @ftp_mkdir($this->conn_id, $path);
00208                 
00209                 if ($result === FALSE)
00210                 {
00211                         if ($this->debug == TRUE)
00212                         {
00213                                 $this->_error('ftp_unable_to_makdir');
00214                         }               
00215                         return FALSE;           
00216                 }
00217 
00218                 // Set file permissions if needed
00219                 if ( ! is_null($permissions))
00220                 {
00221                         $this->chmod($path, (int)$permissions);
00222                 }
00223                 
00224                 return TRUE;
00225         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_FTP::move ( old_file,
new_file 
)

Move a file.

public

Parameters:
string 
string 
Returns:
bool

Definition at line 325 of file Ftp.php.

References rename().

00326         {
00327                 return $this->rename($old_file, $new_file, TRUE);
00328         }

Here is the call graph for this function:

CI_FTP::rename ( old_file,
new_file,
move = FALSE 
)

Rename (or move) a file.

public

Parameters:
string 
string 
bool 
Returns:
bool

Definition at line 292 of file Ftp.php.

References _error(), and _is_conn().

Referenced by move().

00293         {
00294                 if ( ! $this->_is_conn())
00295                 {
00296                         return FALSE;
00297                 }
00298 
00299                 $result = @ftp_rename($this->conn_id, $old_file, $new_file);
00300                 
00301                 if ($result === FALSE)
00302                 {
00303                         if ($this->debug == TRUE)
00304                         {
00305                                 $msg = ($move == FALSE) ? 'ftp_unable_to_rename' : 'ftp_unable_to_move';
00306                                 
00307                                 $this->_error($msg);
00308                         }               
00309                         return FALSE;           
00310                 }
00311                 
00312                 return TRUE;
00313         }

Here is the call graph for this function:

Here is the caller graph for this function:

CI_FTP::upload ( locpath,
rempath,
mode = 'auto',
permissions = NULL 
)

Upload a file to the server.

public

Parameters:
string 
string 
string 
Returns:
bool

Definition at line 238 of file Ftp.php.

References _error(), _getext(), _is_conn(), _settype(), and chmod().

Referenced by mirror().

00239         {
00240                 if ( ! $this->_is_conn())
00241                 {
00242                         return FALSE;
00243                 }
00244 
00245                 if ( ! file_exists($locpath))
00246                 {
00247                         $this->_error('ftp_no_source_file');
00248                         return FALSE;
00249                 }
00250         
00251                 // Set the mode if not specified
00252                 if ($mode == 'auto')
00253                 {
00254                         // Get the file extension so we can set the upload type
00255                         $ext = $this->_getext($locpath);
00256                         $mode = $this->_settype($ext);
00257                 }
00258                 
00259                 $mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
00260                 
00261                 $result = @ftp_put($this->conn_id, $rempath, $locpath, $mode);
00262                 
00263                 if ($result === FALSE)
00264                 {
00265                         if ($this->debug == TRUE)
00266                         {
00267                                 $this->_error('ftp_unable_to_upload');
00268                         }               
00269                         return FALSE;           
00270                 }
00271                 
00272                 // Set file permissions if needed
00273                 if ( ! is_null($permissions))
00274                 {
00275                         $this->chmod($rempath, (int)$permissions);
00276                 }
00277                 
00278                 return TRUE;
00279         }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

CI_FTP::$conn_id = FALSE

Definition at line 35 of file Ftp.php.

CI_FTP::$debug = FALSE

Definition at line 34 of file Ftp.php.

CI_FTP::$hostname = ''

Definition at line 29 of file Ftp.php.

CI_FTP::$passive = TRUE

Definition at line 33 of file Ftp.php.

CI_FTP::$password = ''

Definition at line 31 of file Ftp.php.

CI_FTP::$port = 21

Definition at line 32 of file Ftp.php.

CI_FTP::$username = ''

Definition at line 30 of file Ftp.php.


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