Php ftp Class php ftp class
'', 'Username' =>'', 'password' => '', 'port' => ''...); */public function _ construct ($ config = array () {if (count ($ config)> 0) {$ this-> _ init ($ config );}} /*** FTP connection ** @ access public * @ param array configure array * @ returnboolean */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) * @ paramboolean * @ returnboolean */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) * @ paramint file permission list * @ returnboolean */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 * @ paramstring remote Directory ID (ftp) * @ paramstring upload mode auto | ascii * @ paramint file permission list after upload * @ returnboolean */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) * @ paramstring local Directory ID * @ paramstring download mode auto | ascii * @ returnboolean */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) * @ paramstring New Directory ID * @ paramboolean determine whether to rename (FALSE) or move (TRUE) * @ returnboolean */ 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) * @ returnboolean */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) * @ returnboolean */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) * @ returnboolean */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) * @ returnarray */publ Ic function filelist ($ path = '.') {if (! $ This-> _ isconn () {return FALSE;} return ftp_nlist ($ this-> conn_id, $ path );} /***** disable FTP ** @ access public * @ returnboolean */public function close () {if (! $ This-> _ isconn () {return FALSE;} return @ ftp_close ($ this-> conn_id );} /*** FTP member variable initialization ** @ accessprivate * @ paramarray configuration array * @ returnvoid */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 * @ returnboolean */private function _ login () {return @ ftp_login ($ this-> conn_id, $ this-> username, $ this-> password );} /*** determine the con_id ** @ access private * @ returnboolean */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 * @ returnstring */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 ** @ access private *@ Param string extension * @ returnstring */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 record ** @ access prvate * @ returnboolean */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.Ftp_demo.php
'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');/*End of file ftp_demo.php*//*Location: /htdocs/ftp_demo.php*/
The above is the content of the php ftp class. For more information, please follow the PHP Chinese network (www.php1.cn )!