FTP upload file and download file instance code in PHP

Source: Internet
Author: User
Tags abstract ftp connection ftp upload file rar

First, LycFtpAbstract.class.php FTP base class

The code is as follows Copy Code
<?php
/* 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 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 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 File
*
*/
Public function upload ($remotefile, $localfile) {

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

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

}
?>



Second, LycFtpFtp.class.php implementation class

The code is as follows Copy Code
<?php
/* 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 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 files
*
*/
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 anomaly base class

The code is as follows Copy Code
<?php
/* Author: Bump Mann (LYC)
/* email:jar-c@163.com
/* time:2011-04-22
/*

*/
Class Lyc_exception extends exception{

}
?>


Four, LycFtpException.class.php FTP exception class

The code is as follows Copy Code
<?php
/* 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{

}
?>


V. Test area

The code is as follows Copy Code


<?php
/**
* Upload File
*
*/
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:\wwwroot\testarea\Lyc\Test\test.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";
}

}

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.