I wrote the FTP component to allow for an easy way of uploading files via FTP. This component is easily extensible.
1. Install Pear package Net_ftp or copy the files to your libs directory.
2. Create a config file ftp.php in your config directory for any possible defines.
3. Make a directory FTP in your Zoop installation. Please include the following files:
/*************************
***** ftp_component.php **
*************************/
<?php
/**
* FTP component - Component allows for transferring files using the PEAR package Net_FTP
*
* Copyright (c) 2009 Jayesh Wadhwani, Dominion Enterprises
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* If you have any questions or comments, please email:
* Jayesh Wadhwani
* jayesh.wadhwani@dominionenterprises.com
* http://www.dominionenterprises.com/
*
*
* PHP versions 5
*
* * component_ftp
*
* Please make sure that you have the Pear package Net_FTP installed on your system.
* Alternately you could also download the files and put them in your libs folder.
* @uses component
* @package
* @version $id$
* @copyright 2009 Dominion Enterprises
* @author Jayesh Wadhwani
*/
class component_ftp extends component
{
function component_ftp()
{
}
function getIncludes()
{
return array(
"ftp" => $this->getBasePath() . "/ftp.php"
);
}
}
?>
/*************************
******** ftp.php *********
*************************/
<?php
require_once 'FTP.php';
/**
* A wrapper for the Net_ftp Pear package
*
* The most common application for FTP is to connect and send a file. This is what I
* have implemented. You can extend this class for more functionality.
*
* @author Jayesh Wadhwani
* @copyright 2009 Dominion Enterprises
* @license http://www.gnu.org/copyleft/gpl.html GPL License 2 or later
*/
class ftp
{
//The FTP handle
private $ftp;
//login credentials
private $username;
private $password;
/**
* Instantiation of class and setup
*
* @param string $host e.g. somehost.mysite.com
* @param int $port e.g. 21 default is 21
* @param string $username e.g. somehost.mysite.com
* @access public
*/
function __construct($host, $port = 21, $username, $password){
$this->username = $username;
$this->password = $password;
$this->ftp = new Net_FTP($host, $port);
}
/**
* Use this to upload a file to the host server
*
* @param string $sourceFile
* @param string $destinationFile
* @param bool $overwrite - a value of true will overwrite the destination file.
* @param int $transferMode - FTP_ASCII or FTP_BINARY. Default is FTP_ASCII
* @param int $transferMode - FTP_ASCII or FTP_BINARY. Default is FTP_ASCII
* @access public
*/
function ftpUpload($sourceFile, $destinationFile, $overwrite = true, $transferMode = FTP_ASCII, $passive = true){
//make the connection
$result = $this->ftp->connect();
if (PEAR::isError($result)){
print $result;
}
//login
$result = $this->ftp->login($this->username, $this->password);
if (PEAR::isError($result)){
print $result;
}
//set connection type
if($passive === true){
$this->ftp->setPassive();
}
//upload
$result = $this->ftp->put($sourceFile, $destinationFile, $overwrite, $transferMode);
if (PEAR::isError($result)){
print $result;
}
}
/**
* Close connection on exit
* @access public
*/
function __destruct(){
$this->ftp->disconnect();
}
}
/***************************
*** defaultConstants.php ***
***************************/
<?php
?>
/***************************
*** Example: ***************
***************************/
<?php
$sourceFileName = '/www/files/source.xml';
$destination = '/public_html/destination.xml';
$b = new ftp('somehost.somesite.com', 21, 'username', 'password');
$b->ftpUpload($sourceFileName, $destination, true, FTP_BINARY);
?>