Miscellaneous Code
  Home arrow Miscellaneous Code arrow PHP_FTP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
Case Studies 
iPad Development 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

PHP_FTP
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2002-01-18

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Best Practices: The Integrated Project and Portfolio Management Platform.

    Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan.
    FREE! Go There Now!


    NEW! IBM – Taking Web 2.0 to Work

    David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
    FREE! Go There Now!


    NEW! Addressing software-as-a-service challenges using Tivoli security and WebSphere solutions

    Building a software-as-a-service solution requires addressing a few key technical challenges. In this webcast, we'll focus on the role of IBM Tivoli Directory Server and WebSphere Portlet Factory in creating a Software as a Service solution. We will demonstrate how to use Tivoli Directory Server to prevent the user population of one tenant from accessing the virtual portal and portlet components of another tenant. We will also use the dynamic profile capability of WebSphere Portlet Factory to create multiple highly customized applications from one code base.
    FREE! Go There Now!


    NEW! Evaluate IBM Lotus Sametime Standard V8.0

    Visit IBM developerWorks to download a free trial of the latest release of IBM Lotus Sametime Standard V8.0. Lotus Sametime Standard V8.0 is a platform for unified communications and collaboration that combines security features with an extensible, open solution including integrated Voice over IP, geographic location awareness, mobile clients, and a robust Business Partner community offering telephony and video integration.
    FREE! Go There Now!


    NEW! Evaluate Rational Business Developer V7.1

    Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities.
    FREE! Go There Now!


    NEW! Hacking 101

    Join us for this web seminar to learn how you can defend your web applications from attack. Learn about the 3 most common web application attacks, including how they occur and what can be done to prevent them. We’ll also discuss manual versus automated approaches for scanning and identifying web application vulnerabilities and how IBM Rational AppScan, an automated vulnerability scanner, can help you automate more of what you are doing manually today.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! Rational 'Talks to You' Teleconference Series

    This Fall, IBM Rational talks to you directly through a special teleconference series giving you access to the best minds in IBM Rational - product experts and market thought leaders who will answer your questions during these pre-scheduled telephone conference calls. Register today!
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for Connectivity

    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!


    NEW! Webcast: Calling All Testers! Find Application Vulnerabilities Early in the Development Process Where they are Easier to Fix and Less Risky to your Business

    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!

    MISCELLANEOUS CODE ARTICLES

    - Creating a Web Page Controller with the HMVC...
    - Coding Controllers and Views for the HMVC De...
    - A Sample Web Application with the HMVC Desig...
    - Adding a Class to Parse Views to an HMVC Des...
    - Building a Model Class for the HMVC Design P...
    - Filtering Input Data and Generating HTML For...
    - The HMVC Design Pattern: Working with MySQL ...
    - Dispatching Requests to MVC Triads with the ...
    - Implementing the Hierarchical Model-View-Con...
    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 7 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek