PHP: uploading and downloading files through FTP _ PHP Tutorial

Source: Internet
Author: User
Tags ftp connection
Upload and download file instance code through FTP in PHP. 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. I. LycFtp in php, we can also directly operate ftp, and then use php to implement the same file upload and download functions as ftp, next I will introduce a complete instance.

1. LycFtpAbstract. class. php FTP base class

The code is as follows:
/* 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:
/* 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:
/* 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:
/* 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:


/**
* 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 ";
}

}


Bytes. 1. LycFtp...

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.