FTP operation class for CodeIgniter imitation

Source: Internet
Author: User
Tags ftp connection ftp login codeigniter
Provides various official and user-released code examples for code reference. You are welcome to learn about the FTP operation of CodeIgniter.
/**
* Copy the FTP class of CodeIgniter
* Basic FTP operations:
* 1) login; connect
* 2) Current directory file list; filelist
* 3) Directory change; chgdir
* 4) rename/move; rename
* 5) create a folder; mkdir
* 6) Delete; delete_dir/delete_file
* 7) upload and upload
* 8) download
*
*
*/
Class Ftp {

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

/**
* Constructor
*
* @ Param array configuration array: $ config = array ('hostname' => '', 'username' =>'', 'Password' => '', 'Port' => ''...);
*/
Public function _ construct ($ config = array ()){
If (count ($ config)> 0 ){
$ This-> _ init ($ config );
}
}

/**
* FTP connection
*
* @ Access public
* @ Param array configure the array
* @ Return boolean
*/
Public function connect ($ config = array ()){
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 change
*
* @ Access public
* @ Param string directory ID (ftp)
* @ Param boolean
* @ Return boolean
*/
Public function 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_chgdir: dir [". $ path. "]");
}
Return FALSE;
}

Return TRUE;
}

/**
* Directory generation
*
* @ Access public
* @ Param string directory ID (ftp)
* @ Param int: File Permission list
* @ Return boolean
*/
Public function 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 ID
* @ Param string remote directory identification (ftp)
* @ Param string: auto | ascii
* @ Param int: List of uploaded file permissions
* @ 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:". $ localpath );
}
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 identification (ftp)
* @ Param string local directory ID
* @ 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 [". $ localpath. "]-remotepath [". $ remotepath. "]");
}
Return FALSE;
}

Return TRUE;
}

/**
* Rename/move
*
* @ Access public
* @ Param string remote directory identification (ftp)
* @ Param string new directory ID
* @ Param boolean determine whether to rename (FALSE) 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 an object
*
* @ Access public
* @ Param string File ID (ftp)
* @ Return boolean
*/
Public function delete_file ($ file ){
If (! $ This-> _ 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 A folder
*
* @ Access public
* @ Param string directory ID (ftp)
* @ Return boolean
*/
Public function delete_dir ($ path ){
If (! $ This-> _ isconn ()){
Return FALSE;
}

// Add the backslash '\' to the '/' character of the Directory macro '\'
$ Path = preg_replace ("/(. + ?) \/* $/"," \ 1/", $ path );

// Obtain the directory file list
$ Filelist = $ this-> filelist ($ path );

If ($ filelist! = False and count ($ filelist)> 0 ){
Foreach ($ filelist as $ item ){
// If the folder cannot be deleted, it may be a folder.
// Recursively call delete_dir ()
If (! @ Delete_file ($ item )){
$ This-> delete_dir ($ item );
}
}
}

// Delete a 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 ID (ftp)
* @ Return boolean
*/
Public function chmod ($ path, $ perm ){
If (! $ This-> _ isconn ()){
Return FALSE;
}

// Only PHP5 defines the function for modifying permissions (ftp)
If (! Function_exists ('ftp _ chmod ')){
If ($ this-> debug === TRUE ){
$ This-> _ error ("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 the 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 );
}

/**
* Disable 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 configure the array
* @ Return void
*/
Private function _ init ($ config = array ()){
Foreach ($ config as $ key => $ val ){
If (isset ($ this-> $ key )){
$ This-> $ key = $ val;
}
}

// Special character filtering
$ This-> hostname = preg_replace ('|. +? : // | ', '', $ This-> hostname );
}

/**
* FTP login
*
* @ Access private
* @ Return boolean
*/
Private function _ login (){
Return @ ftp_login ($ this-> conn_id, $ this-> username, $ this-> password );
}

/**
* Determine the 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 extension extensions from file names
*
* @ Access private
* @ Param string directory ID
* @ Return string
*/
Private function _ getext ($ filename ){
If (FALSE === strpos ($ filename ,'.')){
Return 'txt ';
}

$ Extarr = explode ('.', $ filename );
Return end ($ extarr );
}

/**
* Extended definition of FTP transmission mode ascii or binary from the suffix
*
* @ Access private
* @ Param string suffix Extension
* @ Return string
*/
Private function _ settype ($ ext ){
$ Text_type = array (
'Txt ',
'Text ',
'Php ',
'Phps ',
'Php4 ',
'Js ',
'Css ',
'Htm ',
'Html ',
'Phpml ',
'Shtml ',
'Log ',
'Xml'
);

Return (in_array ($ ext, $ text_type ))? 'Ascii ': 'binary ';
}

/**
* Error log records
*
* @ 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 );
}
}



$ Config = array (
'Hostname' => '2017. 160.116.76 ',
'Username' => 'hangye _ lsp_19@192.168.18.19 ',
'Password' => 'tb6bcnhnro ',
'Port' => 52816
);

$ Ftp = new Ftp ();

$ Ftp-> connect ($ config );
$ Ftp-> upload ('localfile. log', 'remotefile. log ');
?>

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.