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! |
Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools. FREE! Go There Now!
|
|
|
|
This tutorial shows new users of IBM WebSphere Business Monitor Version 6.0.2 how to perform the "Hello World" equivalent for monitoring business process applications. It is intended to help you get familiar with the capabilities of the product. FREE! Go There Now!
|
|
|
|
Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository. FREE! Go There Now!
|
|
|
|
Rational Modeling Extension for Microsoft .NET enhances usability for code generation supporting a more intelligent refactoring. The latest enhancements enable organizations with Java and .NET systems and software development maintain architectural integrity across heterogeneous platforms. FREE! Go There Now!
|
|
|
|
Join this Rational Talks to You teleconference on December 11 at 1:00 pm ET to get tips on building your own plugins with Rational Method Composer. Get your questions answered! FREE! Go There Now!
|
|
|
|
Regression testing -- in which code is thoroughly tested to ensure that changes have not produced unexpected results -- is an important part of any development process. But many testing environments neglect the terminal-based applications that still form the backbone of many industries. In this tutorial, you'll learn how the Rational Functional Tester Extension for Terminal-Based Applications works with other Rational Functional Tester to help test terminal-based applications quickly and easily. FREE! Go There Now!
|
|
|
|
Informix Dynamic Server (IDS) Express Edition offers outstanding online transaction processing (OLTP) database performance, while helping to simplify and automate many of the tasks associated with deploying databases for small business applications. IDS 11 further extends the ease of management and applications integration with the Admin API and Scheduler, high availability with Continuous Log Restore for backup server recovery in case of a primary server failure, and column level encryption to protect personal and company private data. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to try the IBM SOA Sandbox for connectivity. The SOA Sandbox for connectivity provides a trial environment with the tooling and components to help you explore how to effectively connect your infrastructure and integrate all of the people, processes and information in your company. Use the hosted sandbox to explore SOA techniques that streamline connecting existing IT assets together, as well as learn how to connect them to new business logic. FREE! Go There Now!
|
|
|
|
Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, where he will overview Rational’s new offerings and programs to help customers accelerate software innovation on System z. He will discuss how these solutions help organizations extend their core business processes toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time. FREE! Go There Now!
|
|
|
|
In this webcast, IBM Rational will discuss the importance of Web application security and will share techniques and best practices to introduce application security testing into current QA processes including: understanding common security vulnerabilities and techniques to integrate security testing with defect tracking and remediation systems in an effort to safeguard sensitive online information. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |