PHP ftp upload files and download file Instance Code _php tutorial

Source: Internet
Author: User
Tags ftp connection ftp file
In PHP We also have the ability to directly operate FTP, and then use PHP to implement the same as FTP file upload and download the file function Oh, let me introduce a complete example.

First, LycFtpAbstract.class.php FTP base class

The code is as follows Copy Code
/* Author: Bump Mann (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 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 authentication 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 Files
*
*/
Public function upload ($remotefile, $localfile) {

}
/**
* Download File
*
*/
Public function Download ($localfile, $remotefile) {

}
/**
* Close Connection
*
*/
Public function Close () {
if (is_string ($this->ftpobj)) {
Ftp_close ($this->ftpobj);
}
}

}
?>



Second, LycFtpFtp.class.php realization class

The code is as follows Copy Code
/* Author: Bump Mann (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 Files
*
*/
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 File
*
*/
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;
}
}
?>


Third, LycException.class.php anomaly base class

The code is as follows Copy Code
/* Author: Bump Mann (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
/* Author: Bump Mann (LYC)
/* email:jar-c@163.com
/* time:2011-04-22
*/
Require_once ' lyc/exception.class.php ';
Class Lyc_ftp_exception extends lyc_exception{

}
?>


Five, the test area

The code is as follows Copy Code


/**
* Upload Files
*
*/
Public Function uploadtest () {
Require_once ' lyc/ftp/ftp.class.php ';
$host =23.64.41.13 '; Host
$user = ' Tguser '; User name
$pwd = ""; Password Port default 21 can also be changed
$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/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";
}

}


http://www.bkjia.com/PHPjc/444624.html www.bkjia.com true http://www.bkjia.com/PHPjc/444624.html techarticle in PHP We also have the ability to directly operate FTP, and then use PHP to implement the same as FTP file upload and download the file function Oh, let me introduce a complete example. One, 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.