For everyone to introduce a copy CodeIgniter FTP class, the function is very powerful, have the need of friends, you can refer to the next. 1, File Upload class ftp.php
', ' username ' + ', ' password ' + ', ' port ' = ' ... '; */Public Function __construct ($config = Array ()) {if (count ($config) > 0) {$this->_init ($config); }}/** * FTP connection * * @access public * @param array Configuration Array * @return Boolean */Public function connect ($config = AR Ray ()) {if (count ($config) > 0) {$this->_init ($config); if (FALSE = = = ($this->conn_id = @ftp_connect ($this->hostname, $this->port))) {if ($this->debug = = = TRUE) {$this->_error ("Ftp_unable_to_connect"); } return FALSE; if (! $this->_login ()) {if ($this->debug = = = TRUE) {$this->_error ("Ftp_unable_to_login"); } return FALSE; } if ($this->passive = = = True) {FTP_PASV ($this->conn_id, true); } return TRUE; }/** * Directory changed * * @access public * @param string directory identifier (FTP) * @param boolean * @return Boolean */Public functio N Chgdir ($path = ", $supress _DEBUG = FALSE) {if ($path = =" OR! $this->_isconn ()) {return falsE } $result = @ftp_chdir ($this->conn_id, $path); if ($result = = = False) {if ($this->debug = = = TRUE and $supress _DEBUG = = False) {$this->_error ("Ftp_unable_to_ch Gdir:dir[". $path."] "); return FALSE; } return TRUE; }/** * Directory generation * * @access public * @param string directory ID (FTP) * @param int file permission list * @return Boolean */Public Fu Nction mkdir ($path = ", $permissions = NULL) {if ($path = = ' OR! $this->_isconn ())} {return FALSE; } $result = @ftp_mkdir ($this->conn_id, $path); if ($result = = = FALSE) {if ($this->debug = = = TRUE) {$this->_error ("ftp_unable_to_mkdir:dir[". $path. "]"); } return FALSE; } if (! Is_null ($permissions)) {$this->chmod ($path, (int) $permissions); } return TRUE; }/** * Upload * * @access public * @param string Local Directory Identity * @param string remote directory ID (FTP) * @param string upload mode auto | | ASCII * @param int uploaded file permission list * @return Boolean */Public function upload ($localpath, $remotepath, $mode = 'Auto ', $permissions = NULL) {if (! $this->_isconn ()) {return FALSE; if (! file_exists ($localpath)) {if ($this->debug = = = TRUE) {$this->_error ("Ftp_no_source_file:". $localpat h); } return FALSE; } if ($mode = = ' auto ') {$ext = $this->_getext ($localpath); $mode = $this->_settype ($ext); } $mode = ($mode = = = ' ASCII ')? Ftp_ascii:ftp_binary; $result = @ftp_put ($this->conn_id, $remotepath, $localpath, $mode); if ($result = = = FALSE) {if ($this->debug = = = TRUE) {$this->_error ("ftp_unable_to_upload:localpath[". $localpath. "]/remotepath[". $remotepath. "]"); } return FALSE; } if (! Is_null ($permissions)) {$this->chmod ($remotepath, (int) $permissions); } return TRUE; }/** * Download * * @access public * @param string remote directory ID (FTP) * @param string Local Directory identity * @param string download mode Auto | | ASCII * @return Boolean/Public Function download ($remotepath, $localpath, $mode = ' auto ') {if (! $this->_isconn ()) {return FALSE; } if ($mode = = ' auto ') {$ext = $this->_getext ($remotepath); $mode = $this->_settype ($ext); } $mode = ($mode = = = ' ASCII ')? Ftp_ascii:ftp_binary; $result = @ftp_get ($this->conn_id, $localpath, $remotepath, $mode); if ($result = = = FALSE) {if ($this->debug = = = TRUE) {$this->_error ("ftp_unable_to_download:localpath["). $localpat H. "] -remotepath[". $remotepath."] "); return FALSE; } return TRUE; }/** * Rename/move * * @access public * @param string remote directory Identity (FTP) * @param string New directory identifier * @param boolean judgment is rename (fals E) or Move (TRUE) * @return Boolean */Public Function rename ($oldname, $newname, $move = FALSE) {if (! $this->_isconn ( ) {return FALSE; } $result = @ftp_rename ($this->conn_id, $oldname, $newname); if ($result = = = False) {if ($this->debug = = = TRUE) {$msg = ($move = = False)? "Ftp_unable_to_rename": "Ftp_unable_to_move"; $this->_error ($msg); } return FALSE; } return TRUE; }/** * Delete files * * @access Public * @param string file ID (FTP) * @return Boolean */Public function Delete_file ($file) {if (! $this-& Gt;_isconn ()) {return FALSE; } $result = @ftp_delete ($this->conn_id, $file); if ($result = = = FALSE) {if ($this->debug = = = TRUE) {$this->_error ("ftp_unable_to_delete_file:file[". $file. "]") ; } return FALSE; } return TRUE; /** * Delete Folder * * @access public * @param string directory identifier (FTP) * @return Boolean */Public function Delete_dir ($path {if (! $this->_isconn ()) {return FALSE; }//Add backslash ' \ ' to the '/' character of the directory macro $path = Preg_replace ("/(. +?) \/*$/"," \\1/", $path); Get list of directory files $filelist = $this->filelist ($path); if ($filelist!== FALSE and Count ($filelist) > 0) {foreach ($filelist as $item) {//If we cannot delete, then it may be a folder//so we hand Call Delete_dir () if (! @delete_file ($item)) {$this->delete_dir ($item); }}}//Delete folder (empty folder) $result = @ftp_rmdir ($this->conn_id, $path); if ($result = = = FALSE) {if ($this->debug = = = TRUE) {$this->_error ("ftp_unable_to_delete_dir:dir[". $path. "]"); } return FALSE; } return TRUE; }/** * Modify file Permissions * * @access public * @param string directory identifier (FTP) * @return Boolean */Public function chmod ($path, $p ERM) {if (! $this->_isconn ()) {return FALSE; The function (FTP) if (! function_exists (' Ftp_chmod ')) {if ($this->debug = = = TRUE) {if}////Only in PHP5 is defined, {$this->_erro R ("Ftp_unable_to_chmod (function)"); } return FALSE; } $result = @ftp_chmod ($this->conn_id, $perm, $path); if ($result = = = FALSE) {if ($this->debug = = = TRUE) {$this->_error ("ftp_unable_to_chmod:path[". $path. "] -chmod[". $perm."] "); return FALSE; } return TRUE; /** * Get directory file list * * @access public * @param string directory ID (FTP) * @return Array */Public function filelist ($path = '.') {if (! $this->_isconn ()) {return FALSE; } return Ftp_nlist ($this->conn_id, $path); }/** * Close FTP * * @access public * @return Boolean*/Public Function close () {if (! $this->_isconn ()) {return FALSE; } return @ftp_close ($this->conn_id); }/** * FTP member variable initialization * * @access private * @param array configuration arrays * @return void */Private Function _init ($config = arr Ay ()) {foreach ($config as $key + $val) {if (Isset ($this-$key)) {$this, $key = $val; }}//special character filter $this->hostname = preg_replace (' |. +?:/ /| ', ', $this->hostname); /** * FTP Login * * @access Private * @return Boolean */Private Function _login () {return @ftp_login ($this->con n_id, $this->username, $this->password); }/** * Judge con_id * * @access Private * @return Boolean */Private Function _isconn () {if (! Is_resource ($this-> ; conn_id) {if ($this->debug = = = TRUE) {$this->_error ("ftp_no_connection"); } return FALSE; } return TRUE; /** * Get suffix extension from file name * * @access private * @param string directory identifier * @return String */Private function _getext ($filen AME) {if (FALSE = = = Strpos ($fiLename, '. ')) {return ' txt '; } $extarr = Explode ('. ', $filename); Return end ($extarr); }/** * Defines FTP transfer mode from suffix extension ASCII or binary * * @access private * @param string suffix extension * @return String */private Func tion _settype ($ext) {$text _type = array (' txt ', ' text ', ' php ', ' Phps ', ' php4 ', ' JS ') , ' css ', ' htm ', ' html ', ' phtml ', ' shtml ', ' Log ', ' xml '); Return (In_array ($ext, $text _type))? ' ASCII ': ' binary '; }/** * ERROR log record * * @access prvate * @return Boolean */Private Function _error ($msg) {return @file_put_contents (' Ftp_err.log ', "date[". Date ("Y-m-d h:i:s"). "] -hostname[". $this->hostname."] -username[". $this->username."] -password[". $this->password."] -msg[". $msg."] \ n ", file_append); }}/*end of File Ftp.php*//*location/apache group/htdocs/ftp.php*/?> 2. Invoking the example
' localhost ', ' username ' + ' root ', ' password ' = ' root ', ' port ' = ' ; $ftp = new FTP (); $ftp- >connect ($config); $ftp->upload (' Ftp_err.log ', ' ftp_upload.log '); $ftp->download (' Ftp_upload.log ', ' ftp_ Download.log ');/*from http://bbs.it-home.org*//*end of file ftp_demo.php*//*location:/htdocs/ftp_demo.php*/?>
|