Alot of people have asked for VERY simple FTP functionality with PHP, so here it is. This class was designed with very simplistic transfers in mind. You simple create the class, get/send a file and then 'kill()' the object. It's that simple. I you find it useful.
By : ryanflynn
<?php
/*
PHP_FTP
Version 0.5
Ryan Flynn (ryan@ryanflynn || DALnet->#php->pizza_milkshake)
Tuesday, June 26 2001
Alot of people have asked for VERY simple FTP functionality with PHP, so here it is.
This class was designed with very simplistic transfers in mind. You simple create the class,
get/send a file and then 'kill()' the object. It's that simple. I you find it useful.
Example:
//Get a file
require("class.ftp.php"); //include library
$f=new PHP_FTP('ftp.somesite.com', 'username', 'password'); //specify connect info
$f->get('html/test.txt', 'c:/php/ftp/blah.txt'); //yes, tested on Windows
$f->kill(); //optional destroy class method
//Send a file
require("class.ftp.php");
$f=new PHP_FTP('ftp.somesite.com', 'username', 'password', 21); //optional port as 4th arg
$f->send('c:/php/ftp/blah.txt', 'html/test.txt'); //yes, tested on Windows
$f->kill();
//how to test for completion
if(!$f->send('c:/php/ftp/blah.txt', 'html/test.txt')){
echo "File sent successfully!";
}else{
echo "Error sending file.";
}
Notes:
Remember to have all permissions set to their appropriate settings before using
this class
*/
class PHP_FTP{
var $server='';
var $username='';
var $password='';
var $port=21;
var $remote_dir='';
function PHP_FTP($server, $username='anonymous', $password='e@mail.com', $port=21){
$this->server=$server;
$this->username=$username;
$this->password=$password;
$this->port=$port;
}
//exterior
function send($filename='', $save_as='', $passive=TRUE){
$conn=$this->return_connection() or die;
@ftp_pasv($conn, $passive);
$this->set_remote_dir(ftp_pwd($conn));
if(!ftp_put($conn, $save_as, $filename, FTP_BINARY)){
@ftp_quit($this->conn);
return false;
}else{
@ftp_quit($this->conn);
return true;
}
return true;
}
function get($filename='', $save_as='', $passive=TRUE){
$conn=$this->return_connection() or die;
@ftp_pasv($conn, $passive);
$this->set_remote_dir(ftp_pwd($conn));
if(!ftp_get($conn, $save_as, $this->remote_dir.$filename, FTP_BINARY)){
@ftp_quit($this->conn);
return false;
}else{
@ftp_quit($this->conn);
return true;
}
}
function kill(){
if($this->conn)
$this->disconnect();
unset($this);
}
//interior
function return_connection(){
$conn_id = @ftp_connect($this->server, $this->port) or die("Could not connect to FTP");
$login_result = @ftp_login($conn_id, $this->username, $this->password) or die("Could not login to FTP");
return $conn_id;
}
function set_remote_dir($dir){
$x = substr($dir, (strlen($dir)-1));
if($x != "/" && $x != "\\")
$dir.="/";
$this->remote_dir=$dir;
}
}
?>
/* example */
require("class.ftp.php");
$f=new PHP_FTP('ftp.somesite.com', 'username', 'password', 21); //optional port as 4th arg
$f->send('c:/php/ftp/blah.txt', 'html/test.txt'); //yes, tested on Windows
$f->kill();
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Miscellaneous Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
CakePHP is a stable production-ready, rapid-development aid for building Web sites in PHP. This "Cook up Web sites fast with CakePHP" series shows you how to build an online product catalog using CakePHP. FREE! Go There Now!
|
|
|
|
Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager. FREE! Go There Now!
|
|
|
|
Download a free trial version of IBM Rational Developer for System i V7.1, which provides a complete development environment for traditional i5/OS application development. IBM Rational Developer for System i is a new eclipse-based workstation offering for i5/OS application development that provides a comprehensive Integrated Development Environment for edit/compile/debug of traditional RPG/COBOL/C/C++ i5/OS applications. FREE! Go There Now!
|
|
|
|
Analysts, architects, and developers who have existing COBOL or PL/I skills and want to extend those skills to deploy new workloads on the mainframe can use the IBM Enterprise Modernization Sandbox for System z to find hands-on walkthroughs of common real world scenarios. The scenarios provide examples of how to rapidly design, create, assemble, test, and deploy high-quality Web, Web services, portal, and SOA applications for IBM CICS, IBM IMS, and IBM WebSphere Application Server. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered! FREE! Go There Now!
|
|
|
|
Learn the basics of the IBM Customer Information Control System (CICS). With a hands-on exercise, learn how to get your first CICS application up and running on your desktop using TXSeries V6.1 for Windows. The tutorial shows you how to download and install a free trial version of TXSeries V6.1. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Method Composer V7.2 which helps you deliver customized yet consistent process guidance to your project teams and IT organization, and includes the latest version of IBM Rational Unified Process (RUP), which has provided process guidance to teams since 1996. FREE! Go There Now!
|
|
|
|
User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
|
|
|
|
IBM Lotus Notes 8 provides a wide range of developers the ability to provide customized, integrated user interfaces via composite applications and via custom sidebar and toolbar plug-ins. This webcast provides you with tips and techniques to use with out-of-the-box capabilities of Lotus Notes 8, and survey how you can share useful components within your own company and within a larger community. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |