In php, we can also directly operate ftp, and then use php to implement the same file upload and download functions as ftp. Here is a complete example.
1. LycFtpAbstract. class. php FTP base class
The Code is as follows: |
Copy code |
<? Php /* Author: mongoman (lyc) /* Email: jar-c@163.com /* Time: 2011-04-22 */
Abstract class Lyc_Ftp_Abstract {
Protected $ ftpobj = null; Protected $ host = ''; Protected $ user = 'anonymous '; Protected $ pwd = ''; Protected $ mode = FTP_BINARY; Protected $ port = 21; Protected $ timeout = 90;
Protected $ pasv = TRUE;
Protected function init (){
} /** * Establish an ftp connection * */ Protected function connect (){ $ This-> ftpobj = @ ftp_connect ($ this-> host, $ this-> port, $ this-> timeout ); If (null ==$ this-> ftpobj ){ Require_once 'lyc/Ftp/Exception. class. php '; Throw new Lyc_Ftp_Exception ("ftp error: Couldn't connect to $ this-> host "); } } /** * Establish an ssl ftp connection * */ Protected function connectSsl (){ $ Ftpobj = @ ftp_ssl_connect ($ this-> host, $ this-> port, $ this-> timeout ); If (null = $ ftpobj ){ Require_once 'lyc/Ftp/Exception. class. php '; Throw new Lyc_Ftp_Exception ("ftp error: Couldn't connect to $ this-> host "); } } /** * Login verification ftp and setup Mode * */ Protected function login (){
If (@ ftp_login ($ this-> ftpobj, $ this-> user, $ this-> pwd )){ Ftp_pasv ($ this-> ftpobj, $ pasv );
} Else { Require_once 'lyc/Ftp/Exception. class. php '; Throw new Lyc_Ftp_Exception ("ftp error: Couldn't login to $ this-> host "); } } /** * Upload a file * */ Public function upload ($ remotefile, $ localfile ){
} /** * Download an object * */ Public function download ($ localfile, $ remotefile ){
} /** * Close the connection. * */ Public function close (){ If (is_string ($ this-> ftpobj )){ Ftp_close ($ this-> ftpobj ); } }
} ?>
|
Ii. LycFtpFtp. class. php implementation class
The Code is as follows: |
Copy code |
<? Php /* Author: mongoman (lyc) /* Email: jar-c@163.com /* Time: 2011-04-22 /* */ Require_once 'lyc/Ftp/Abstract. class. php '; Class Lyc_Ftp_Ftp extends Lyc_Ftp_Abstract {
Public function _ construct ($ host, $ user, $ pwd, $ mode = FTP_BINARY, $ port = 21, $ timeout = 90, $ pasv = TRUE ){ $ This-> host = $ host; $ This-> user = $ user; $ This-> pwd = $ pwd; $ This-> mode = $ mode; $ This-> port = $ port; $ This-> timeout = $ timeout; $ This-> pasv = $ pasv; $ This-> init ();
} Protected function init (){
$ This-> connect (); $ This-> login ();
} /** * Upload a file * */ Public function upload ($ remotefile, $ localfile ){
$ Res = ftp_nb_put ($ this-> ftpobj, $ remotefile, $ localfile, $ this-> mode, ftp_size ($ this-> ftpobj, $ remotefile )); While ($ res = FTP_MOREDATA ){ $ Res = ftp_nb_continue ($ this-> ftpobj ); } If ($ res! = FTP_FINISHED ){ Return FALSE; } Return TRUE; } /** * Download an object * */ Public function download ($ localfile, $ remotefile ){ Ftp_set_option ($ this-> ftpobj, FTP_AUTOSEEK, FALSE ); $ Res = ftp_nb_get ($ this-> ftpobj, $ localfile, $ remotefile, $ this-> mode, ftp_size ($ this-> ftpobj, $ localfile )); While ($ res = FTP_MOREDATA ){ $ Res = ftp_nb_continue ($ this-> ftpobj ); } If ($ res! = FTP_FINISHED ){ Return FALSE; } Return TRUE; } } ?>
|
Iii. LycException. class. php exception base class
The Code is as follows: |
Copy code |
<? Php /* Author: mongoman (lyc) /* Email: jar-c@163.com /* Time: 2011-04-22 /*
*/ Class Lyc_Exception extends Exception {
} ?> |
Iv. LycFtpException. class. php FTP exception class
The Code is as follows: |
Copy code |
<? Php /* Author: mongoman (lyc) /* Email: jar-c@163.com /* Time: 2011-04-22 */ Require_once 'lyc/Exception. class. php '; Class Lyc_Ftp_Exception extends Lyc_Exception {
} ?> |
V. Test Zone
The Code is as follows: |
Copy code |
<? Php /** * Upload a file * */ Public function uploadTest (){ Require_once 'lyc/Ftp. class. php '; $ Host = 23.64.41.13 '; // host $ User = 'tguser'; // user Name $ Pwd = ""; // you can change the default password port to 21. $ Ftp = new Lyc_Ftp_Ftp ($ host, $ user, $ pwd ); $ Res = $ ftp-> upload('test.rar ', "F: wwwroottestareaLycTesttest.rar "); If (! $ Res ){ Echo "upload failure "; }
}
Public function downloadTest (){ Require_once 'lyc/Ftp. class. php '; $ Host = 33.64.41.135 '; $ User = 'tguser '; $ Pwd = ""; $ Ftp = new Lyc_Ftp_Ftp ($ host, $ user, $ pwd ); $ Res = $ ftp-> download ("c: test.rar", "test.rar "); If (! $ Res ){ Echo "download failure "; }
}
|