Implementation code of FTP class like CodeIgniter

Source: Internet
Author: User
Tags ftp connection ftp login codeigniter
Implementation code of FTP class like CodeIgniter
We will introduce you to the FTP class of CodeIgniter, which is very powerful and can be used as a reference for anyone who needs it.

1. file Upload ftp. php

     '', 'Username' =>'', 'password' => '', 'port' => ''...); */public function _ construct ($ config = array () {if (count ($ config)> 0) {$ this-> _ init ($ config );}} /*** FTP connection ** @ access public * @ param array configure 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 identifier (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 ID (ftp) * @ param string upload mode 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 ID (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 ID (ftp) * @ param string New Directory ID * @ param boolean determines whether to rename (FALSE) or move (TRUE) * @ re Turn 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 file ** @ 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 folder ** @ access public * @ param string directory identifier (ftp) * @ return boolean */public function delete_dir ($ path) {if (! $ This-> _ isconn () {return FALSE;} // add the backslash '\' $ path = preg_replace ("/(. + ?) \/* $/"," \ 1/", $ path); // Get the directory file list $ filelist = $ this-> filelist ($ path ); if ($ filelist! = False and count ($ filelist)> 0) {foreach ($ filelist as $ item) {// if we cannot delete, it may be a folder // so we call e_dir () recursively 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 the file permission ** @ access public * @ param string directory identifier (ftp) * @ return boolean */public function chmod ($ path, $ perm) {if (! $ This-> _ isconn () {return FALSE;} // the function (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;}/*** obtain the directory file list ** @ access public * @ param string Directory ID (ftp) * @ ret Urn 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 );} /*** initialize FTP member variable ** @ access private * @ param array configure 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 suffix from file name ** @ access private * @ param string Directory ID * @ return string */private function _ getext ($ filename) {if (FALSE = strpos ($ filename ,'. ') {return 'txt';} $ extarr = explode ('. ', $ filename); return end ($ extarr);}/*** extend the definition from the suffix to the FTP transmission mode ascii or binary ** @ Ccess private * @ param string extension * @ return string */private function _ settype ($ ext) {$ text_type = array ('txt ', 'text', 'php ', 'phps', 'php4 ', 'js', 'css', 'htm', 'html', 'phpml', 'shyml', '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. call example

      'localhost',   'username' => 'root',   'password' => 'root',   'port' => 21    );$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*/?>

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.